Thread: Error message in removing duplicate strings

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    50

    Error message in removing duplicate strings

    Hi,

    My program removes the duplicate string(s) and outputs the unique strings but as soon as it does I get an error message saying stack around the variable buffer was corrupted?

    Code:
    #include<stdio.h>
    #include <string.h>
    
    
    int main(void) 
    {
          char buffer[4][4]={"cat","mat","cat","hat"};
          int i, j, k;
          int a=4;
     
          for (i = 0;i < a; i++) 
          {
               for (j = i + 1; j < 4;) 
               {
                    if (strcmp(buffer[j],buffer[i])==0)
                    {
                         for (k = j; k < a; k++) 
                         {
                         strcpy(buffer[k],buffer[k+1]);
                         }
    
                   a--;
                   } 
    
             else
             j++;
             }
         }
     
       for (i = 0; i < a; i++) 
       {
          printf("%s", buffer[i]);
       }
     
       return (0);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    When k = 3; then k+1 is 4 which will be undefined access.

    strcpy(buffer[k],buffer[k+1]);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    I advise you to use an assignment of NULL as an indication that the string is removed.

    I advise you to check previous strings for uniqness, not next.

    I advise you to create array of pointers, not two-dimensional array of chars.

    I advise you to use puts instead of printf (in such simple cases, of course).

    Code:
    const char* buffer[4] = {
       "cat",
       "mat",
       "cat",
       "hat"
    };
    
    // skipped
    
    for( i = 0; i < 4; ++i ) {
        if( buffer[i] ) { puts(buffer[i]); }
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing duplicate elements from an array
    By Pulock2009 in forum C Programming
    Replies: 3
    Last Post: 11-06-2013, 01:25 PM
  2. removing a duplicate entry
    By csharp100 in forum C Programming
    Replies: 4
    Last Post: 04-20-2012, 10:02 AM
  3. removing duplicate words while inputting string
    By vlad_i in forum C Programming
    Replies: 1
    Last Post: 03-08-2011, 10:08 PM
  4. Removing duplicate values from std::list
    By Phenom in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2010, 12:59 PM
  5. Removing duplicate items from vector
    By 7heavens in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2010, 05:38 AM