0

I need help in removing duplicate strings. My code is almost there, it outputs the unique strings then crashes, see for yourself. How can I stop this from happening?

#include <stdio.h> #include <string.h> int main(void) { char array[4][4]={"cat","mat","sat","mat"}; int i, j, k; int a=4; for (i = 0;i < a; i++) { for (j = i + 1; j < 4;) { if (strcmp(array[j],array[i])==0) { for (k = j; k < a; k++) { strcpy(array[k],array[k+1]); } a--; } else j++; } } for (i = 0; i < a; i++) { printf("%s", array[i]); } return (0); } 
1
  • 1
    Where does it crash? Are you getting a segfault? Have you tried a debugger like gdb? Commented Aug 9, 2014 at 22:45

3 Answers 3

2

If you stepped through your program with a debugger you'd find out that the following is your problem:

strcpy(array[k],array[k+1]); 

When k == 3 you're attempting to access an element outside the bounds of array for the second argument of strcpy:

array[k+1] 
Sign up to request clarification or add additional context in comments.

Comments

1

Your approach is not good. When you find a matched string you copy all tail of the array in the new position.

Here is a more simple and clear approach

#include <stdio.h> #include <string.h> #define N 4 int main(void) { char s[N][N] = { "cat", "mat", "sat", "mat" }; int i, j; j = 0; /* current osition where a unique string will be placed */ for ( i = 0; i < N; i++ ) { int k = 0; while ( k < j && strcmp( s[k], s[i] ) != 0 ) k++; if ( k == j ) { if ( j != i ) strcpy( s[j], s[i] ); j++; } } for ( i = 0; i < j; i++ ) printf( "%s ", s[i] ); puts( "" ); return 0; } 

The output is

cat mat sat 

Simply copy paste and investigate.:)

Comments

0

It is better to use strncmp that strcmp. You don't have to iterate through every character of the string as strncmp compares strings tills the their end (null terminated strings). From the documentation (man strncmp on unix systems) of the strncmp:

The strncmp() function compares not more than n characters. Because strncmp() is designed for comparing strings rather than binary data, characters that appear after a `\0' character are not compared.

Try this code:

char array[4][4]={"cat","mat","sat","mat"}; int i, j; int a = 4; for (int i = 0; i < a; i++) { bool unique = true; for (int j = i + 1; j < a; j++) { if (strncmp(array[i], array[j], a) == 0) { unique = false; break; } } if (unique) { printf("%s\n", array[i]); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.