Thread: problem with strcmp

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    problem with strcmp

    I have two concerns in the below code.
    (1) If I replace gets (str1) with scanf (%d, str1) it doesn't work.
    (2) I entered first string as "tina" and second string as "leena is god" so the first string length was "4", the second one was "12". So it should be "str1 is less than str2" but output was "str1 is greater than str2".
    I wonder why it shows it is greater when in fact it is lesser than str2 .

    Code:
    int main(void)
    {
    int str1, str2, i;
    clrscr ();
    printf ("Please enter the string1\n");
    gets (str1);
    printf ("Your string1 length is %d\n", strlen(str1));
    printf ("Please enter the string2\n");
    gets (str2);
    printf ("Your string2 length is %d\n", strlen(str2));
    i = strcmp(str1, str2);
    if (i = 0)
          printf ("str1 is equal to str2");
    if (i < 0)
          printf ("str1 is less than str2");
    if (i > 0)
          printf ("str1 is greater than str2");
    getch ();
    return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Don't use gets(), period. It is depreciated and only exists to maintain backward compatibility with (very old) code. Use fgets() instead, it does the same thing (just you need to specify the input stream and a max length).

    Strcmp() returns a value equivalent to the difference in ascii value of the first character which differs. Qv.

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    So, "tina" vs. "leena"; 't' has an ascii value of 116, 'l' of 108. 116 - 108 = 8, which if you check exactly, should be what strcmp() returned. It has nothing to do with the string length. "leena" vs. "tina" would be 108-116 = -8.

    Fscanf("%s") captures a string, but it stops on spaces. Again, fgets() is probably more what you want here. But you can define custom captures for scan functions, eg:

    Code:
    fscanf(stdin, "%256[a-zA-Z 0-9]", ...)
    Will capture up to 256 characters, but it stops when it hits the first character not part of the capture group (alphanumeric plus the space).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    If that compiles now, you should start including the proper headers and looking at the big long list of warnings that appear when you compile it. Those two things you mention are your two smallest problems.

    Also, if that code prints any of the last three outputs throw your compiler away now, because its stupid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this a problem with strcmp?
    By daynorb in forum C Programming
    Replies: 6
    Last Post: 11-29-2011, 09:59 AM
  2. problem with strcmp
    By byebyebyezzz in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2011, 10:20 PM
  3. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  4. strcmp problem
    By cstudent in forum C Programming
    Replies: 3
    Last Post: 04-15-2008, 09:48 AM
  5. Problem with strcmp()....
    By LightsOut06 in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 12:30 PM