Thread: Strings and Structs (remove duplicates)

  1. #31
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Tater, have you been hanging around politicians lately?
    Nope... just been messed over one more time than I'm willing to put up with.

  2. #32
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In this for loop, you can eliminate the code which refers to strcmp(). You already have tested for it once. No more tests are needed. Do it once, do it right. One shot, one kill.

    Code:
    	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;
    	}
    @Tater: Yep! I can tell.

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ChristianB, an index is just an index. The name is not important at all. Use something descriptive if you can, but just using i is a very long-standing idiom in C. Using a bunch of indexes, seems to have you a bit confused.

    Think of all your indexes as all the same thing - simple counters of iteration. That's all.

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If the first name is a duplicate of another name, that's OK, right? Only if both first and last names are duplicates should it be rejected?

  5. #35
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    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 = 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; 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(ndx = 0; ndx < MAX_LEN; ndx++){
    	      if (Equal(names[ndx], name) != 1){
    		    strcpy(names[ndx].first, name.first);
    		    strcpy(names[ndx].last, name.last);
    	      }
    		}
    
    	
    	for (ndx = 0; ndx < MAX_LEN; ndx++){
    		printf("%s %s\n", names[ndx].first, names[ndx].last);
    	}
    	
    	return 0;
    }
    so this is what I have so far. It doesn't print anything out. I have to be at work soon. it seems silly that i could fail a class for this, even though i've completed the rest of the course work.

    i know that i'm killing the loop somewhere, which is why my last printf isn't printing anything out.

    as far as your question goes, yes if the entire name is a duplicate then it's rejeted. a duplicate first name, or a duplicate last name is fine though.
    Last edited by christianB; 07-25-2011 at 02:59 PM.

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    NOPE! Sorry, didn't catch your other changes.

    Your outer for loop needs to have a different index name than ndx. Use numNames or i or whatever you want, but don't use ndx, that is needed for the inner for loop.
    Last edited by Adak; 07-25-2011 at 03:29 PM.

  7. #37
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... like this....

    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) 
      { return (strcmp (p1.first, p2.first) == 0 && strcmp (p1.last, p2.last) == 0); }
    
    
    int main () 
      { Name names[MAX_LEN],  name;
         int ndx = 0, numNames = 0;
         while(numNames < MAX_LEN)
            { scanf("%s %s", name.first, name.last);
               getchar();
               if (strcmp name.first, "END") == 0 && strcmp (name.last, "END") == 0)
                    break;  
              for(ndx = 0; ndx < numNames; ndx++)
                { if (Equal(names[ndx], name))
                    break; }
              if (ndx == NumNames)       
                {  strcpy(names[numNames].first, name.first);
                   strcpy(names[numNames].last, name.last);
                   numNames++;  } }
    
    	
         for (ndx = 0; ndx < NumNames; ndx++)
           { printf("%s %s\n", names[ndx].first, names[ndx].last); }
    	
        return 0; }

    That should be pretty close...

    (Mods... This guy's online course is totally bogus and I'm not going to let him fail because of errors in the courseware and stupid size restrictions on his code!)
    Last edited by CommonTater; 07-25-2011 at 03:42 PM. Reason: #$^ double = sign! AGAIN!

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This works, but is a bit more verbose than Taters.

    Good luck with your course, Christian.

    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 = 1;
       if (strcmp (p1.first, p2.first) == 0 && strcmp (p1.last, p2.last) == 0)
           i = 0;
       return i;
    
    }
    int main () {
       Name names[MAX_NAMES],  name;
       int ndx, numNames = 0, count=0, good=0;
       
       for(numNames = 0; numNames < MAX_NAMES; numNames++){
          printf("Enter a first and last name.\n");
          scanf("%s%s", name.first, name.last);
    	
          if ((strcmp (name.first, "END") == 0) && (strcmp (name.last, "END")) == 0) 
             break;   
          for(ndx = 0, good=1; ndx < MAX_NAMES; ndx++) {
             if (Equal(names[ndx], name)==0) {
                good=0;
             }
          }      
          if(good==1) {
            strcpy(names[count].first, name.first);
    	strcpy(names[count].last, name.last);
            ++count;
          }
       }
       for (ndx = 0; ndx < count ; ndx++){
          printf("%s %s\n", names[ndx].first, names[ndx].last);
       }
       return 0;
    }

  9. #39
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    This works, but is a bit more verbose than Taters.
    Yeah well... you always did talk too much

    (Just kidding, my friend.)

  10. #40
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CommonTater View Post
    Yeah well... you always did talk too much

    (Just kidding, my friend.)
    Just because my posts can be compared to "War and Peace", everybody says that.

    No idea why. < LOL >

  11. #41
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Just because my posts can be compared to "War and Peace", everybody says that.

    No idea why. < LOL >
    Extraneous verbosity predisposes disproportionate misinterpretation vis a vis one's intended audience.

  12. #42
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by CommonTater View Post
    Ok... like this....

    Code:
    ...

    That should be pretty close...

    (Mods... This guy's online course is totally bogus and I'm not going to let him fail because of errors in the courseware and stupid size restrictions on his code!)
    I took a screen shot for prosperity

  13. #43
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CommonTater View Post
    Extraneous verbosity predisposes disproportionate misinterpretation vis a vis one's intended audience.
    These Canadians are always talking this French stuff, behind your back. < rofl >

  14. #44
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    These Canadians are always talking this French stuff, behind your back. < rofl >
    Mais oui mon ami... c'est la mode Canadienne!

  15. #45
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    I took a screen shot for prosperity
    Planning a little blackmail are we? Well... good luck with that

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