Reputation: 13
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);
}
Upvotes: 0
Views: 94
Reputation: 311018
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.:)
Upvotes: 1
Reputation: 2031
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]);
}
}
Upvotes: 0
Reputation: 8861
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]
Upvotes: 2