Sunday, July 12, 2009

How do you compare strings with spaces in c programming?

every time i compare strings with spaces, it only compares the first word before the space.

How do you compare strings with spaces in c programming?
If you're using strcmp() or strncmp(), they both handle spaces (blanks) the same as any other character. If you have previously parsed those strings with strtok(), it may have substituted NULL characters (000) for the spaces, which would terminate the comparison.





Here's a note from the Visual C/C++ Version 7 runtime library description of strtok:





"Returns a pointer to the next token found in strToken [the parameter]. They [variants of strtok] return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered."





Hope that helps.
Reply:use ascii code of blank space to compare the string


No comments:

Post a Comment