0

Using C, I have an array of strings, some are duplicates. I'm trying to count the number of unique strings. Code:

 for(i=0; i<size; i++){
        flag=0;
        if(strcmp(" ", tArr[i])!=0){
            for(j=i; j<size; j++){
                if(strcmp(tArr[i], tArr[j])==0){
                    if (flag<1){
                        flag=1;
                        k++;
                    }
                    strcpy(tArr[j], " ");
                }
            }
        }
    }
  1. First for loop goes through the whole array

  2. Set flag to zero for each iteration

  3. Using blanks as another flag, if an index is blank, that means that word has already been counted, move to next index

  4. Compare that index to every index after it, thus j=i

  5. If the secondary index matches the first

  6. If that word has not been found yet

  7. Trigger the flag

  8. Add to k, the unique word count

  9. Set every remaining instance of that word to a blank space, to match line 3

    Instead of setting k to the unique word count, it gets set to the total word count. Using printed flags I was able to tell that the function makes it all the way through, even to after line

  10. I still can't tell how it makes it to line 8 every iteration of the outermost for loop.

1
  • 1
    The inner loop should be for(j=i+1; j<size; j++), no? Commented Mar 10, 2015 at 1:51

1 Answer 1

5

Adapting your code, try something like this:

for (i = 0; i < size; i++) {
    for (j = i + 1; j < size; j++)
        if (strcmp(tArr[i], tArr[j]) == 0)
            break;
    if (j == size)
        unique_count++;
}

There should be no need to destroy the duplicates if you are just counting, however if you still wish to do that I would suggest a null string instead of one containing a space.

Sign up to request clarification or add additional context in comments.

3 Comments

@iharob While I appreciate the edit, I don't think stylistic changes to code are called for, especially when it was adapting original code from the OP. Reverting.
@iharob Style is highly subjective, I would never go so far as to edit someones style in a post just as I would not in the workplace - in fact in the workplace one should stay consistent with the rest of the project regardless of ones own stylistic preferences. Let that reflect how I care about safety and quality.
You make a good point there, but it's just that the lack of whitespace in code makes it hard to differentiate an operand from an operator and makes it all like a huge single token, which of course doesn't bother the compiler, but it requires extra effort to read the code, so it really bothers me, a lot. And I didn't change the style of the code, just added whitespace to make it clearer although since style is subjective as you said that could be considered as part of the style so you are right, and I am sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.