hi how can you do a case insensitive string compare ? i'm using uvision3 , c51 version 8. the string.h don't know stricmp or strlwr. can i compile my own function ?
thanks gamba
how can you do a case insensitive string compare ?
A simple solution is to convert all letters in the strings to one case (upper or lower), and then do a regular string compare.
hi it seems that the C51 is not supporting any function of lower or upper case convert. please advice.
thanks everyone!
it seems that the C51 is not supporting any function of lower or upper case convert.
Assumed that you are using the ASCII code, then converting between upper and lower case is a simple matter of adding or subtracting an offset from the character.
For example:
'A' = 0x41 'a' = 0x61 'a' - 'A' = 0x20
So in order to convert an upper case letter to lower case, just add 0x20. To convert lower to upper case, just subtract 0x20. Of course, you need to make sure that the character in question is actually a letter and not some other character, or you'll convert your string to garbage.
en.wikipedia.org/.../Ascii
How about just:
c = (c >= 'a' && c <= 'z') ? c - ('a'-'A') : c;
Yes, that's a neat way to convert from lower- to upper-case; it is left as an exercise for the student to apply a similar expression to make a case-insensitive compare...
stricmp() and its relatives are not standard C library functions. If I recall correctly, they're defined by one of the POSIX standards.
FreeBSD source for their C library can be found here. www.freebsd.org/.../
i understand that i am supposed to add the stricmp() to the c51 program somehow but i failed.
file.C(101): error C267: 'stricmp': requires ANSI-style prototype
file.C(101): warning C206: 'stricmp': missing function-prototype
this is all i get.
i used my own function to receive input and transform it to lower-case but when i call the function i get the same errors as above.
if i change the function to void it runs, but if i deliver a variable to the function i get the error above.
the code is
#include <stdio.h> #include <string.h> #include <REG52.H> #include <stdlib.h> #include <ctype.h> void main (void) { char *textBody; char *textbody2; char *password; char *lwrtext2; char *checkbody; char *path= "hello"; textBody = (char*)strchr(path,'\0'); textBody++; strcpy(textbody2,textBody); lwrtext2 = (char *)lwrbuffer(textbody2); if (strcmp(lwrtext2, "password" )==0) printf("password OK!"); scanf("%d", &n); } void lwrbuffer(checkbody) { int i; char *lwrtext; char *lowertextBody; i=0; for (;i <= strlen(checkbody);i++) { checkbody[i] = (checkbody[i] >= 'a' && checkbody[i] <= 'z') ? checkbody[i] - ('a'-'A') : checkbody[i]; lowertextBody[i] = checkbody[i]; } strcpy (lwrtext,lowertextBody); return (lwrtext) ; }
Any thoughts ?
Well done for using the correct tags to post your source code, but is that really how you lay it out?
Your indenting is quite bizarre! Have you used TABs? Don't use TABs, use spaces - TABs are entirely unreliable!
void lwrbuffer(checkbody) { int i; char *lwrtext; char *lowertextBody; i=0; for (;i <= strlen(checkbody);i++) { checkbody[i] = (checkbody[i]>='a' && checkbody[i]<='z') ? checkbody[i]-('a'-'A') : checkbody[i]; lowertextBody[i] = checkbody[i]; } strcpy (lwrtext,lowertextBody); return (lwrtext) ; }
You haven't defined the type of the parameter to lwrbuffer - 'C' will make an assumption, but it isn't what you want;
Why do you initialise i to zero in a separate statement outside the for loop?
You are converting each element of the checkbody array in-place - there is no need for all the messing about with copying to lowertextBody and lwrtext;
You are treating lowertextBody and lwrtext as if they were arrays - but they are just pointers;
You are returning a value in a void function.
Note that this is all standard 'C' - nothing specific to Keil nor the 8051 here!
View all questions in Keil forum