Thread: Strings and Structs (remove duplicates)

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    35

    Strings and Structs (remove duplicates)

    Hello, I have one more problem this semester that I need to complete or else i automatically fail my class and it is stumping me. I don't even know how to begin. Here is my code that is already filled in for me. I have commented out what I need to type in.

    I have to fill in the code below so that the program reads in a series of first name/last name pairs, stopping when it hits an entry of END END. It prints out the names encountered, removing any duplicates, in the same order they were originally entered. It does not print the END END. You may assume no names are longer than 10 characters, and that there are at most 20 names other than END END.

    Code:
    #define MAX_NAMES 10
    #define MAX_LEN 20
    
    ///define struct
    
    typedef struct {
       char first[MAX_NAMES];
       char last[MAx_NAMES];
    } Name;
    
    
    
    int Equal(Name p1, Name p2) {
       ///write function 80 characters max
    
    }
    
    int main() {
       Name names[MAX_NAMES], name;
       int ndx, numNames = 0;
    
       scanf("%s%s",name.first,name.last);
    
    /////600 characters max for the main. 
    
       
    
    
       return 0;
    }
    can anyone give me some direction? I appreciate any help!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, so get to work... We're not going to do it for you.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    not asking anyone to do it for me, i'm just asking for some direction. I'm just not sure how to start based on the variables I'm given. I'm just hoping for some advice. Thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read in all the names. For each name, check if it has been previously stored; if so, don't store it.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by christianB View Post
    not asking anyone to do it for me, i'm just asking for some direction. I'm just not sure how to start based on the variables I'm given. I'm just hoping for some advice. Thanks.
    First... I would start by going to my teacher and asking straight up "What's with this 80 characters max bullsmut you keep dumping on us?" ... really this is PROGRAMMING... "Stupid Restrictions" is down the hall...

    Well, ok... you've got a skeleton of a program there... You've got a list of names the user will be entering...
    Now sit down and THINK about the problem... you need to pour through a list of names eliminating duplicates...
    What's the very first thing you need to do?
    And what's next?

    If you take the time to actually understand the problem... most often the solution presents itself.

    And, trust me on this one... There is no programmer anywhere, no matter how experienced or intelligent, who can code the solution to a problem he or she does not understand.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    ok, so first i need to scan in an array of name pairs. So i have this:

    Code:
    #include <stdio.h>
    #define MAX_NAMES 10
    #define MAX_LEN 20
    
    typedef struct {
    	char first[MAX_NAMES];
        char last[MAX_NAMES];
    }Name;
    
    
    int Equal (Name p1, Name p2) {
    
    }
    
    
    int main () {
    	Name names[MAX_NAMES],  name;
    	int ndx, numNames = 0;
    
    	scanf("%s%s", name.first, name.last);
    	for(ndx = 0; ndx < MAX_LEN; ndx++){
    	   scanf("%s%s", names[ndx].first, names[ndx].last);
            if (names[ndx].first == "END" && names[ndx].last == "END")
    			break;
    	}
    
    	printf("%d", ndx);
    
        return 0;
    }
    This doesn't even work though. If I enter "END END" the loop is supposed to break, and an int should be printed, but I just keep getting a prompt, no matter how many times I hit enter.
    Last edited by christianB; 07-25-2011 at 10:47 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can not use the comparison operator ( == ) to compare C-strings. You must use strcmp().

    Jim

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not really sure what you're doing with that first scanf in main, you can probably eliminate it. Also, you can't compare strings with ==. You need to use the strcmp function. Google should turn up the documentation and some examples. Otherwise, it looks like you're on the right track. Keep on keepin on.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    i forgot about those functions. I can't delete that printf though, it's part of the code that my professor provided for us. That's partly why I am stuck. I don't understand why I need that scanf, since in the test input and output my professor shows, that first name pair is part of the list of names i need to check.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    I'm not really sure what you're doing with that first scanf in main, you can probably eliminate it. Also, you can't compare strings with ==. You need to use the strcmp function. Google should turn up the documentation and some examples. Otherwise, it looks like you're on the right track. Keep on keepin on.
    Since we're obviusly dealing with the "Nutty Professor" here... it strikes me that he probably wants our friend to get the new item into the struct, check if it's a duplicate and then add it to something like an array of structs only if it's unique. (I'm thinking "array" instead of "linked list" since there's no pointer in the struct.)

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yeah, I think you're right Tater. It also occurred to me, after a more careful reading, that the prof likely wants the students to write the Equal function:
    Code:
    int Equal (Name p1, Name p2) {
        // do your strcmp of first and last here, return true (1) if both names match, false (0) for all others
    }
    then use it for determining duplicate names.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    ok, so I think i'm supposed to keep name.first and name.last to build new array. i will compare all of the names in names[MAX_NAMES]. if all of the names in the array are differnet from name, then "name" becomes the first name of the array, and the old first pair of names of the array become "name". Then it will repeat. All the names are checked against "name", and if none of them are the same as "name", it will become the second name of the array and the old second name of the array will become "name".

    this would be my code for that.

    Code:
    	if (Equal(names[ndx], name) != 0){
    		strcpy(names[numNames].first, name.first);
    	    strcpy(names[numNames].last, name.last);
    		strcpy(name.first, names[ndx].first);
    		strcpy(name.last, names[ndx].last);
    	}
    i dont know quite how the loops would work though.

    this is my equal function
    Code:
    int Equal (Name p1, Name p2) {
    	int equal;
    	if (strcmp (p1.first, p2.first) == 0 && strcmp (p1.last, p2.last) == 0)
    		equal = 0;
    	return equal;
    
    }
    does this seem like the correct logic?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Very close... your strcpy is backwards, you want to use ... strcpy(strTO, strFROM)...

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    ok, so this is what I have.
    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_NAMES 10
    #define MAX_LEN 20
    
    typedef struct {
    	char first[MAX_NAMES];
        char last[MAX_NAMES];
    }Name;
    
    
    int Equal (Name p1, Name p2) {
    	int i = 0;
    	if (strcmp (p1.first, p2.first) == 0 && strcmp (p1.last, p2.last) == 0)
               i = 0;
            else 
               i = 1;
    	return i;
    
    }
    
    
    int main () {
    	Name names[MAX_NAMES],  name;
    	int ndx, numNames = 0;
        scanf("%s%s", name.first, name.last);
    	for(ndx = 0; ndx < MAX_LEN && names[ndx].first != "END" && names[ndx].last != "END"; ndx++){
    	   scanf("%s%s", names[ndx].first, names[ndx].last);
           if (strcmp (names[ndx].first, "END") == 0 && strcmp (names[ndx].last, "END") == 0)
    			break;
    	}
    
    	for (numNames = 0; numNames < MAX_NAMES; numNames++){
    	   for (ndx = 0; ndx < MAX_NAMES; ndx++){
    	      if (Equal(names[ndx], name) != 0){
    			   
    	   strcpy(name.first, names[numNames].first);
    	   strcpy(name.last, names[numNames].last);
    	   strcpy(names[ndx].first, name.first);
    	   strcpy(names[ndx].last, name.last);
    	     }	  
    	   }
    	}
    
    
    
    	for (numNames = 0; numNames < MAX_NAMES; numNames++){
    		printf("%s%s\n", names[numNames].first, names[numNames].last);
    		if (strcmp (names[numNames].first, "END") == 0 && strcmp (names[numNames].last, "END") == 0)
    			break;
    	}
    	
    	return 0;
    }
    it removes only one duplicate and prints out END END though.
    Last edited by christianB; 07-25-2011 at 01:12 PM.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But your Equal() function is over 80 characters long, and it is always returning 0.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive remove duplicates from a string
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-20-2009, 02:02 AM
  2. Strings and structs
    By Roger in forum C Programming
    Replies: 1
    Last Post: 11-07-2009, 04:06 PM
  3. Remove puncuation from strings in a set?
    By Dragoon_42 in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2004, 03:04 AM
  4. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM
  5. Really basic remove duplicates from array
    By Chris Fowler in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 10:35 PM