Thread: strcmp

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    111

    strcmp

    Problem: If only one word is inserted, only output one word


    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
    	char my_string[15];
    	char my_string_2[15];
    
    
    	char empty[1] = "";
    
    
    	printf("> ");
    
    
    	// If only one word is inserted, only output one word
    
    
    	int i = 1;
    
    
    	do
    	{
    		scanf("%s %s", my_string, my_string_2);
    		if(strcmp(my_string_2, empty) == 0)
    		{
    				printf("You inserted one string: %s\n", my_string);
    		}
    
    
    		else
    		{
    			printf("You inserted two strings\n");
    		}
    	} while(i == 1);
    
    
    	return 0;
    }
    Hello all, this is a portion to a larger program i'm working on. Basically if one string is inserted, i would like printf to output the one string. Currently scanf is hanging. I was wondering about this line:

    Code:
    char empty[1] = ""
    I've tried it without the quotes, also adusting array size. Also this line:

    Code:
    		
    if(strcmp(my_string_2, empty) == 0)
    I've tried using != in the strcmp function as well as < 1 and > 1 without any luck. I also I have a question, would it be possible to include '\0' or '\n' in the strcmp function.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,485
    Currently scanf is hanging.
    Yes your scanf() requires two entries and it will wait until you enter both entries.

    Instead of using strcmp() perhaps you should try strlen() to check the length of the string.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    111
    Looks like strlen() requires to entries too

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,485
    No the problem is that scanf() requires two entries, are you entering two strings?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    Code:
    #include <stdio.h>
     
    int main() {
        char line[200], str[50], str2[50];
     
        printf("> ");
        fgets(line, sizeof line, stdin);
     
        int n = sscanf(line, "%49s %49s", str, str2);
     
        if (n == 1)
            printf("You entered one string: %s\n", str);
        else if (n == 2)
            printf("You entered two strings: %s %s\n", str, str2);
     
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2017, 01:36 AM
  2. Help with strcmp.
    By liam.h in forum C Programming
    Replies: 9
    Last Post: 11-13-2011, 05:41 AM
  3. strcmp
    By _arjun in forum C Programming
    Replies: 5
    Last Post: 09-21-2011, 04:07 PM
  4. Help: strcmp
    By LuizCPFL in forum C Programming
    Replies: 5
    Last Post: 12-11-2008, 03:09 AM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

Tags for this Thread