Tuesday, July 14, 2009

Matching lowercase and uppercase in c++?

I'm writing a program a class for making menus just for practice, I use some code (shown below) to match what the user inputed to an array of names. My problem is that if the user enters "help", and the menu item's internal name is "Help", then my program doesn't find the menu item. How can I enter "help" or any other string (lowercase and uppercase letters) and have it match with the internal name "Help"?

Matching lowercase and uppercase in c++?
use strcasecmp(char *s1, char *s2)





It returns 0 if the 2 strings are the same except for differences in case.





for example:





strcasecmp("Hello", "hELLO") returns 0
Reply:A simple way to do this is to convert all string to lower or upper case before doing any comparisons, this results in a case insensitive comparison. Most string libraries have functions o do this (I think it's tolower or toupper in C and C++).
Reply:you can use the toupper/tolower function to convert chars in uppercase/lowercase .. and then compare it.


No comments:

Post a Comment