Error message in removing duplicate strings

This is a discussion on Error message in removing duplicate strings within the C Programming forums, part of the General Programming Boards category; Hi, My program removes the duplicate string(s) and outputs the unique strings but as soon as it does I get ...

  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
    2,577
    When k = 3; then k+1 is 4 which will be undefined access.

    strcpy(buffer[k],buffer[k+1]);
    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    zub
    zub is offline
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    96
    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, 12: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, 09: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, 04:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21