Finding unique elements in an string array in C

后端 未结 5 2158
轻奢々
轻奢々 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:56

    Why are you starting second loop from 1?

    You should start it from i+1. i.e.

    for(j=i+1;j<20;j++) 
    

    Like if the list is

    abc
    def
    abc
    abc
    lop
    

    then

    when i==4

    tmp="lop"

    but then the second loop starts which is from 1 to 19. This means it will get a value of 4 too at one stage, and then

    data[4], which is "lop", will be same as tmp. So although "lop" is unique but it will be flagged as repeated.

    Hope it was helpful.

提交回复
热议问题