Finding unique elements in an string array in C

后端 未结 5 2182
轻奢々
轻奢々 2021-01-05 16:39

C bothers me with its handling of strings. I have a pseudocode like this in my mind:

char *data[20]; 

char *tmp; int i,j;

for(i=0;i<20;i++) {
  tmp = da         


        
5条回答
  •  春和景丽
    2021-01-05 16:44

    char *data[20];
    int i, j, n, unique[20];
    
    n = 0;
    for (i = 0; i < 20; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            if (!strcmp(data[i], data[unique[j]]))
               break;
        }
    
        if (j == n)
            unique[n++] = i;
    }
    

    The indexes of the first occurrence of each unique string should be in unique[0..n-1] if I did that right.

提交回复
热议问题