Thread: Using strncmp to compare input with string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    29

    Using strncmp to compare input with string

    I have been working with this program for a long time now and I dont know how I can make it work.

    The code is as follows:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main ()
    {
        char input[80];
        int i;
        char stop[] = "STOP";
        printf("Type a name:\n");
        char array[][80]={"Kalle","Peter","Maxum","Einsten",
            "Patrick Zen","Patrick Mark","Patricia Yrsa","Yrsan Dabom"};
        for(;;)
        {
            scanf("%s",&input);
            if(strcmp(stop,input)==0)
            {
                break;
            }
            for(i=0;i<8;i++)
            {
                if(strncmp(array[i],input,(unsigned)strlen(input))==0)             {
                    printf("%s\n",array[i]);
                }
            }
        }
    
        
        
    }
    This is my output.

    Code:
    Type a name: Patrick M
    Patrick Zen
    Patrick Mark
    Maxum
    If I type "Patrick M" I only want it to show me who has those 9 chars in it. Why doesnt it show me that?

    Like I want my output to be like:

    Code:
    Type a name: Patrick M
    Patrick Mark

    It like ignores the space and start over?

    Tell me if you want to explain it more.

    - Lovelace

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    scanf() for strings will stop at the first whitespace character, so if you're typing "Patrick M" it actually only reads in "Patrick", then it prints the 2 Patrick matches, then on the next loop it reads in the leftover "M" and prints the Maxum match for that. Check out fgets() instead for input.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    29
    Quote Originally Posted by itsme86 View Post
    scanf() for strings will stop at the first whitespace character, so if you're typing "Patrick M" it actually only reads in "Patrick", then it prints the 2 Patrick matches, then on the next loop it reads in the leftover "M" and prints the Maxum match for that. Check out fgets() instead for input.
    After a few hours I thought I figured out how this fgets() worked. But now when I push ENTER it writes all strings in my array. Why is that?

    I realised that fgetc put \n in the end, so i found a way to remove it.

    And is this how fgetc should be used? Or am I doing it wrong?
    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main ()
    {
        char stop[]={"STOP"};
        int i;
        char array[][80]={"Kalle","Peter","Maxum","Einsten",
            "Patrick Zen","Patrick Mark","Patricia Yrsa","Yrsan Dabom"};
        
        printf("Type a name:\n");
        char buffer[30];
        for(;;)
        {
            fgets(buffer,30, stdin);
            for ( i = 0; i < 30; i++ )
            {
                if ( buffer[i] == '\n' )
                {
                    buffer[i] = '\0';
                    break;
                }
            }
            if(strcmp(stop,buffer)==0)
            {
                break;
            }
            for(i=0;i<8;i++)
            {
                if(strncmp(array[i],buffer,strlen(buffer))==0)            
                {
                    printf("%s\n",array[i]);
                }
            }
    
    
        
        
        }
    }
    Last edited by lovelace; 10-10-2011 at 05:58 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by lovelace View Post
    But now when I push ENTER it writes all strings in my array. Why is that?
    If you compare zero characters in these two strings "ABC" and "DEF" how many non-matches do you get?
    Answer: zero.
    Therefore, the string matches for the first zero characters.

    Tim S.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    29
    Quote Originally Posted by stahta01 View Post
    If you compare zero characters in these two strings "ABC" and "DEF" how many non-matches do you get?
    Answer: zero.
    Therefore, the string matches for the first zero characters.

    Tim S.
    Ah yes that makes great sense. I should have figured that out myself, but thanks for clearing that out for me.
    I added this and now it is working. Cheers!

    Code:
    if(strncmp(array[i],buffer,strlen(buffer))==0 && strlen(buffer) > 0)
    Cheers.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by lovelace View Post
    Code:
    if(strncmp(array[i],buffer,strlen(buffer))==0 && strlen(buffer) > 0)
    If you did want to do it that way, you would be better off reversing those two checks.
    Code:
    if( strlen( buffer ) > 0 && strncmp( ... ) )
    That way you would only strncmp if you had something to compare. It doesn't actually matter in a simple program like this, but it's good to get in the habit of thinking about the order that you check things, because you can short-circuit your comparisons and save your program time.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by itsme86 View Post
    scanf() for strings will stop at the first whitespace character
    More specifically, %s will stop at the first whitespace after it has read something that was not whitespace. Assuming you have not specified a width specifier as well.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Edit: itsme86 is also correct; so do his suggestion first.

    This is wrong!
    Code:
    scanf("%s",&input);
    This is right in my opinion
    Code:
    scanf("%s",input);
    You need to turn on Compiler warnings and not ignore the warnings.

    Code:
    main.c|14|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[80]'|
    main.c|29|warning: control reaches end of non-void function|
    Tim S.
    Last edited by stahta01; 10-10-2011 at 03:05 PM.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Why do you not simply use
    Code:
    if( strcmp(array[i],buffer)==0 )

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    And even better, you can just check to see if the first character in buffer is a \0 instead of counting each character in buffer and seeing if that count is greater than 0:
    Code:
    if(buffer[0] != '\0' && strncmp( ... ))
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Replies: 4
    Last Post: 06-04-2009, 01:36 AM
  3. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  4. Checking to see if the input has all of the compare line
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2003, 05:12 PM
  5. compare input doubles
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2002, 08:57 PM