有些时候,我真的需要比较两个CHAR,并从我所知道的是没有函数在C来比较两个CHAR(也许我错了),因为这个,我决定写一个我自己的。 该功能工作正常,但我不知道如果还行,或者有一些问题。 我决定问你关于这个,如果我的功能没问题。 下面是程序:Char比较功能在C
#include<stdio.h>
#include<string.h>
int chrcmp(const char chr1, const char chr2);
int main(void){
char firstChar = 'a';
char secondChar = 'a';
if(chrcmp(firstChar, secondChar) == 0){
printf("We have a Match\n");
}else{
printf("There was no match Found.\n");
printf("%c",chrcmp(firstChar, secondChar));
}
return 0;
}
int chrcmp(const char chr1, const char chr2){
size_t lenght1, lenght2;
char s1[2] = {chr1 , '\0'}; /* Convert chr1 to string */
char s2[2] = {chr2 , '\0'}; /* Convert chr2 to string */
lenght1 = strlen(s1); /* Store lenght of first String */
lenght2 = strlen(s2); /* Store lenght of second String */
if(lenght1 == 1 && lenght2 == 1){ /* Checking if both strings have the same size (1) */
if(strcmp(s1,s2) == 0){ /* Compare both strings */
return 0; /* Match Found! */
}else{
return 1; /*No Match!;*/
}
}else{
return 1; /*To many chars Found!;*/
}
}
如果有一些问题,或者如果我做错了什么我没有ideeas。
与你的问题没有关系,实际上是一个挑剔的问题,但这个词是“长度”,而不是“长度”。 – szczurcio
英语不是我自己的语言,但我认为你已经注意到了, – Michi
是的,我没有试图批评你,或者让你感觉不好,只是一个友好的笔记,所以你知道未来的正确拼写:) – szczurcio