C 字符串数组中不同的出现次数

C 字符串数组中不同的出现次数,c,arrays,string,count,C,Arrays,String,Count,我在获取字符串数组中不同事件的总数时遇到了一些问题,假设我有一个这样的结构,它已经为每个成员填充了品牌。结构[1]。brand=“Fiat”等 typedef结构{ char品牌[25]; }结构 CarStruct-struct[100]; void countDifferentBrands(){ char*cPtr; cPtr=malloc(sizeof(char)*100); int i,j,计数=0; 对于(i=0;i您不需要cPtr——您可以直接比较不同的CarStruct项 对于列表

我在获取字符串数组中不同事件的总数时遇到了一些问题,假设我有一个这样的结构,它已经为每个成员填充了品牌。结构[1]。brand=“Fiat”等

typedef结构{
char品牌[25];
}结构
CarStruct-struct[100];
void countDifferentBrands(){
char*cPtr;
cPtr=malloc(sizeof(char)*100);
int i,j,计数=0;

对于(i=0;i您不需要
cPtr
——您可以直接比较不同的
CarStruct

对于列表中的每个项目,开始时假设它是唯一的。对照列表中以前的每个项目检查它;如果找到匹配项,请注意它不是唯一的并将其中断。如果退出循环时
unique
仍然为true,则增加计数

for ( i = 0; i < 100; i++ ) {
  int unique = 1;

  for ( j = 0; j < i; j++ ) {
    if ( strcmp( struct[i].brand, struct[j].brand ) == 0  ) {
      unique = 0;
      break;
    }
  }

  if (unique)
    ++count;
}
(i=0;i<100;i++)的
{
int unique=1;
对于(j=0;j
非常感谢,非常好,干净简单。
for ( i = 0; i < 100; i++ ) {
  int unique = 1;

  for ( j = 0; j < i; j++ ) {
    if ( strcmp( struct[i].brand, struct[j].brand ) == 0  ) {
      unique = 0;
      break;
    }
  }

  if (unique)
    ++count;
}