Question:
How do remove case sensitivity in c language?
Totsie
2011-10-14 02:32:03 UTC
i want my program to compare strings, but i don't want it to consider the case of the letters (uppercase / lowercase).
e.g. motor and MOtor should be considered the same.
i.e. motor=MOtor
or
strcmp(motor,MOtor) should be 0.
Six answers:
2011-10-14 02:53:44 UTC
#include

#include



static int strcasecmp(const char *s1, const char *s2)

{

        while(tolower((unsigned char) *s1) == tolower((unsigned char) *s2)) {

                if(*s1++ == '\0')

                        return 0;

                s2++;

        }

        return (unsigned char) *s1 < (unsigned char) *s2 ? -1 : 1;

}



int main(void)

{

        const char *s1[] = { "abcdefg", "ABCDEFG", "AbCdEfG", "aa", 0 };

        const char *s2[] = { "abcdef", "abcdefg", "AbCdEfG", "bb", 0 };

        size_t index;



        for(index = 0; s1[index]; index++)

                printf("%s and %s %smatch\n", s1[index], s2[index],

                strcasecmp(s1[index], s2[index]) == 0 ? "" : "don't ");

        return 0;

}



@Ratchetr: Standard C doesn't have a stricmp function.
?
2016-11-16 09:00:31 UTC
Strcmp Case Insensitive
2015-08-17 00:28:58 UTC
This Site Might Help You.



RE:

How do remove case sensitivity in c language?

i want my program to compare strings, but i don't want it to consider the case of the letters (uppercase / lowercase).

e.g. motor and MOtor should be considered the same.

i.e. motor=MOtor

or

strcmp(motor,MOtor) should be 0.
Ratchetr
2011-10-14 04:43:25 UTC
There is no need to reinvent the wheel here.

C comes with a function that does that: stricmp



stricmp("motor","MOtor"); will return 0.
2011-10-14 02:44:35 UTC
First convert the string to upper {or lower]}case (any characters already in upper {or lower} case are left as they are):



/* assuming your string is 80 characters long */





#include

#include



int main(void)

{



char str[80];

int i;



printf("Enter a string: ");

gets(str);



for( i = 0; str[ i ]; i++)

str[ i ] = toupper( str[ i ] );



printf("%s\n", str); /* uppercase string */



for(i = 0; str[ i ]; i++)

str[i] = tolower(str[ i ]);



printf("%s\n", str); /* lowercase string */



return 0;

}
roger
2011-10-14 08:16:15 UTC
you want:



int strcasecmp(const char *s1, const char *s2) -- case insensitive version of strcmp().





http://www.cs.cf.ac.uk/Dave/C/node19.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...