Finding a match in an array using strcmp
im trying to compare words of an array using strcmp.Im trying to get each word that app开发者_开发技巧ears more than once in the array to print out only once, so i can determine the number of unique words.I know what its doing wrong as when it searches the array it prints out each copy it finds, for example if the word "the" is in the array 4 times, it will print out 'the' 3 times and when string1
goes to the next location where 'the' is, it will print out 2 times and so on.
Convert your char arrays to std::string
and instead of printing them, put them into an std::set
. Then print each element in the set.
good that you added declarations,
now from what it looks it seems as if words[][] is redundant and makes things unnecessary complicated. if you are only interested in getting unique words, instead just process what comes back from strtrok by building up a dictionary with the encountered words
a dictionary could be something as simple as a max sized array containing unique words, and an index that starts at 0 when array is empty, whenever strtok returns a word, go through the array and look for the word using your strcmp, if it doesn't exist add it at the end of the array then increment then index.
and bob is your uncle :)
精彩评论