Thread: Read in n, then n words, and check to see if first word is repeated.

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    8

    Read in n, then n words, and check to see if first word is repeated.

    Read in n, then n lastnames, and check to see if the first in the list is ever repeated again.

    Here's what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    int strcmp(char *w1, char *w2);
    
    
    int main() {
        int j, n;
        char string1[30], string2[30];
        
        printf("Enter n, followed by n last names (each last name must be a single word): ");
        scanf("%d", &n);
        
        for(j=0; j<n; j++)
        {
            scanf("%s", string1);
        }
    
    
    
    
        if(strcmp(string1, string2))
            printf("First name in the list is repeated\n");
        else
            printf("First name in the list is not repeated\n");
            
        system("pause");
        return 0;
    }
    I see the problem is that it lies within n amount of string that I am not going through every string to compare to the first one.

    How will do compare every string to the first one?


    Current output:
    3
    alex
    alex
    alex
    Not repeated
    ------------------
    3
    alex
    ash
    peter
    Not repeated

    correct input should be:
    5 Reagan Bush Clinton Bush Obama
    First name in list is not repeated.
    ------------------------------
    4 Bush Clinton Bush Obama
    First name in list is repeated.


    Thanks
    Last edited by 5360; 09-23-2013 at 07:56 PM.

  2. #2
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    You need to set a flag. Are you in Lobo's class? lol set repeat as an int and initialize it to 0.. Then in your if statement for the strcmp say if(strcmp(str1,str2,)==0) then repeat=1

    Outside of the loop make another if statement that says if(repeat==1) printf("your first word repeats.")

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    8
    Never Mind!!!
    Figured it out!!!
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-14-2013, 08:16 AM
  2. Words in a Word
    By amb110395 in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2011, 12:51 PM
  3. To check if a sequence is repeated in a string
    By chatterjee in forum C Programming
    Replies: 6
    Last Post: 08-18-2006, 09:40 PM
  4. Check for repeated digits!
    By CrackerJack in forum C Programming
    Replies: 5
    Last Post: 11-08-2003, 11:37 PM
  5. how to delete the repeated word
    By icable in forum C Programming
    Replies: 1
    Last Post: 05-11-2003, 06:56 PM