Hey guys,
I was wondering about the function strcmp(), does the function compare word with spaces? eg: If I have two same words "Harith Javed"; will it match both words??
Hey guys,
I was wondering about the function strcmp(), does the function compare word with spaces? eg: If I have two same words "Harith Javed"; will it match both words??
Yes, it will.
strcmp() will keep comparing characters until it finds a difference or reaches the end of the string (the NULL terminator). Spaces aren't anything special to it -- it'll compare the space just like any other character.
Thanks
strcmp compares strings, not words
if you have 2 strings containing same words but delimitered by different characters (like space and 2 spaces, or space in one string and \t in another) - strcmp will return non-zero value since some chars that are not part of the words differ.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Didn't work? What did you try?
I'm going to guess that you read a line with spaces by using fgets(), then tried to compare the string with strcmp() and it failed.
The answer, my young apprentice, is to look at the \n that fgets() places in the input buffer.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.