如何删去重复的字符串记录?
这是之前的记录:name[ ][] age[ ] *n
-------------------------------
Homer 45 6
Marge 22
Homer 42
Bart 14
Marge 21
Lisa 8
里面有重复的名字,要求把后面重复的记录删去,保留前面的。删去记录后是:
name[ ][] age[ ] *n
----------------------
Homer 45 4
Marge 22
Bart 14
Lisa 8
小弟写的该程序,不知道错在哪里,请指正,谢谢。
#include<stdio.h>
#include <string.h>
/*remove duplicate records*/
main()
{
char name[6][31]={"Homer", "Marge", "Homer", "Bart", "Marge", "Lisa"};
int age[6]={45, 33, 22, 38, 29, 8};
int n;
n=removeDuplicates( name[6][31], age[6], &n );
printf("\n%d", n);
}
int removeDuplicates( char name[6][31], int age[6], int *n )
{
int i = 0, j;
for (i = 0; i < 6; i++)
{
if (strcmp(name[i], name[i + 1])==1)
{
for ( j = i; j < 6; j++)
{
strcpy(name[j], name[j + 1]);
strcpy(name[j + 1], " ");
}
}
}
for (i = 0; i < 6; i++)
printf("%s\n", name[i]);
*n=i;
return *n;
}