Thread: how to compare 2 strings

  1. #1
    Registered User
    Join Date
    Jul 2016
    Location
    Netherlands
    Posts
    3

    how to compare 2 strings

    hi i am new to programming in c ,
    and i wanne know how to compare 2 strings
    one string should be true keyboard input "gets(a);
    and the other string i wanne read from a .txt file with fgets(b,30,f)

    if (strcmp(a,b)==0) dont work cause the value of strcmp is 1 ore -1
    how to set it to 0 and see if the strings are equal ?
    sorry for my bad english .

    greetings GMK0000
    Last edited by GMK0000; 07-28-2016 at 09:37 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    See, gets and fgets work differently. The first ignores the newline character while the second adds it to the string. I suggest using fgets for both the console and file input. Like this:
    Code:
    fgets(a, 30, stdin);
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jul 2016
    Location
    Netherlands
    Posts
    3
    thanx for your reaction i am gonne try it out tonight
    p.s.

    you really devoted your life to program ? hahaha
    if so let me add you as a friend

  4. #4
    Registered User
    Join Date
    Jul 2016
    Location
    Netherlands
    Posts
    3
    works perfectly thanx mate.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You really should forget that you've even heard of the gets() function. This function can never be safely used and has even been removed from the current C standard.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare two strings
    By cable in forum C Programming
    Replies: 3
    Last Post: 02-07-2012, 09:12 AM
  2. compare strings
    By jamal in forum C Programming
    Replies: 6
    Last Post: 11-27-2011, 08:09 PM
  3. How can I compare strings?
    By errigour in forum C Programming
    Replies: 9
    Last Post: 11-16-2010, 05:54 PM
  4. Compare Two Strings?
    By Paul22000 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2008, 05:09 PM
  5. Trying to compare to strings...
    By imortal in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2003, 12:27 PM

Tags for this Thread