Thread: strcmp();

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    strcmp();

    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??

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    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.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Thanks

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Harith View Post
    I was wondering about the function strcmp(), ..., will it match both words??
    Take a friendly piece of advice: It is best for you, I would say, to test your wonders. Write a small program that implements your wondering and find out yourself.
    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

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by std10093 View Post
    Take a friendly piece of advice: It is best for you, I would say, to test your wonders. Write a small program that implements your wondering and find out yourself.
    Ya, thats the best way to find out, and I already tried that but it didn't work. So was just confirming.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Didn't work? What did you try?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by Salem View Post
    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.
    Well you got that right, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what exactly does strcmp() do?
    By MattJ812 in forum C++ Programming
    Replies: 6
    Last Post: 01-16-2011, 11:02 PM
  2. strcmp
    By bob1223 in forum C Programming
    Replies: 4
    Last Post: 08-29-2010, 08:24 AM
  3. strcmp()
    By norhos in forum C Programming
    Replies: 15
    Last Post: 03-15-2008, 09:15 AM
  4. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  5. strcmp
    By Rob J in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2004, 11:44 PM

Tags for this Thread