Monday, July 27, 2009

C question?

What's wrong with the following program? I got syntax error.


I am just trying to make 2 strings to the same case, and compare them. So that strcasecmp ignores case while checkong whether 2 strings are the same. Thanks if u can help





int strcasecmp(const char* str1, const char* str2)


{


int cursor = 0;


while(str1[cursor] != '\0') {


if(str1[cursor] %26gt;= 'a' %26amp;%26amp; str1[cursor] %26lt;= 'z') {


str1[cursor] -= 32;


}


cursor++;


}





int cursor2 = 0;


while(str2[cursor2] != '\0') {


if(str2[cursor2] %26gt;= 'a' %26amp;%26amp; str2[cursor] %26lt;= 'z') {


str2[cursor2] -= 32;


}


cursor2++;


}





return strcmp(str1, str2);

C question?
I think you may need to move the definitionof cursor2 to the top of the file.


Make sure you subtract (char) 32, or, if you want to be more platform-independent, (char) 'a'-'A'.


Did you include string.h?


A lot of this stuff is supported in libraries:


if(strX[cursorX]%26gt;='a'%26amp;%26amp;strX[cursorX]%26lt;=...


if(!isupper(strX[cursorX]))


int isupper (char): #include %26lt;ctype.h%26gt;


strX[cursorX]-=32;


strX[cursorX]=toupper(strX[cursorX]);


char toupper(char): #include %26lt;ctype.h%26gt;





And your whole function itself:


int stricmp(const char *, const char *): #include %26lt;string.h%26gt;


No comments:

Post a Comment