Like Tree6Likes
  • 2 Post By std10093
  • 1 Post By smokeyangel
  • 3 Post By Salem

strcmp();

This is a discussion on strcmp(); within the C Programming forums, part of the General Programming Boards category; Hey guys, I was wondering about the function strcmp(), does the function compare word with spaces? eg: If I have ...

  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
    535
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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.
    vart and Salem like this.
    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
    535
    Didn't work? What did you try?
    Salem likes this.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10: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, 10:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21