September 8th, 2003, 11:05 AM
who can teach me wat is the function for accept a string with igone case ?
September 8th, 2003, 11:10 AM
Huh? Can you be more specific?
September 8th, 2003, 12:53 PM
He might mean he wants to convert a string which contains capitals to lower case, which is sort of ignoring case specified.
Here is a basic example for ASCII to convert higher case letters to lower case...
int main(int argc, char **argv)
char str = "HihihihiHihihiHiHHH", *ptr;
for(ptr = str; ptr < str+sizeof(str); ptr++)
if(*ptr >= 'A' && *ptr <= 'Z')
*ptr += 32;
September 8th, 2003, 12:57 PM
ctype.h has a function to convert characters to lower case. You can use it to write a function to convert a whole string.
The string is converted in place, the returned pointer is for convenience in experssions such as:
char* strtolower( char* string )
int i = 0 ;
while( string[i] != '\0' )
string[i] = tolower( string[i] ) ;
return( string ) ;
Although I do not recomment that coding style!:eek:
if( strcmp( strtolower( input_str ), "quit" ) == 0 ) ...
September 8th, 2003, 01:01 PM
Second thought. Most compiler libraries have a case-insensitive version of strcmp(). It is not standard, so its name varies from compiler to compiler.
are common names for such a function.
September 8th, 2003, 01:36 PM
strcasecmp() is also available in strings.h on my box.
I just included the code to convert case manually so he fully understands it, not that it's complex in any way. I think people should fully understand every function they use.
September 9th, 2003, 05:18 AM
oh sorry i forgot i need the function is in c++ not in c
September 9th, 2003, 12:51 PM
I assume therefore that you are using the C++ string class rather than C style strings? If not then it does not matter since C is a subset of C++.
Example from http://www.msoe.edu/eecs/cese/resources/stl/string.htm
Note that it uses the C tolower function as a pointer-to-function parameter in the transform() function.
string str22 = "This IS a MiXed CaSE stRINg";
transform (str22.begin(),str22.end(), str22.begin(), tolower);
cout << "[" << str22 << "]" << endl; // [this is a mixed case string]
transform() is in <algorithm>. You'll need to include <algorithm>, <string>, and <cctype> (or <ctype.h>).
Example function(untested, fix it yourself if broken!)
#include <algorithm> ;
#include <string> ;
#include <cctype> ;
string& string_to_lower( string& convert )
transform (convert.begin(), convert.end(), convert.begin(), tolower ) ;
return( convert ) ;