Hi, everyone!

I have to make a function which would get a c-string of an inputted text and it would output the number of times each unique word occurs in it.

Of course, I must use c-strings [yeah yeah, I know but that is what the assignment is].

Here's my code:

void analyzeWordOccurrence(char* inputtedText)
    //the function gets a C-String of text inputted from the user
    //it tokenizes the words, and then counts the number of times each
    //word occurs in the inputtedText
    map<char*,unsigned> wordCounts = map<char*,unsigned>();

    //tokenize the words and then add unique values to wordCounts
    //if there is already such a value, increase the corresponding count by 1
    char* tokenPtr = strtok(inputtedText," ");  //begin tokenization of sentence

    while (tokenPtr != NULL)                    //i.e. there is a token to analyze
        //extract the word
        cout << "DEBUG: tokenPtr: " << tokenPtr << endl;
        char aToken[strlen(tokenPtr)];

        cout << "DEBUG: aToken is: " << aToken << endl;
        cout << "wordCounts.count(aToken) is: " << wordCounts.count(aToken) << endl;
        if (wordCounts.count(aToken) == 0)        //the token is not in the map
            wordCounts.insert( pair<char*,unsigned>(aToken,1));
            wordCounts[aToken]+=1;                              //token is there and increment count

        tokenPtr = strtok(NULL," ");

    cout << "\nDEBUG: Done counting. The counts are: " << endl;
    for (auto& kv : wordCounts)
        cout << kv.first << " has value " << kv.second << endl;

The issue is with the line "wordCounts.count(aToken) == 0.

Somehow it gives an output of 0 for the first word, but for the second word, it thinks that it is equal to the first word that I tokenized and put in map, and thus goes to the "else" statment, incrementing it to a 2 from 1.

Here's my console output from debugging:

Please <Enter> a giant line of text as input.
Do not be afraid if your input spans multiple lines on this console as this is OK (as long as the number of characters inputted is less than 500).

<Enter> here: malechi likes to exercise
DEBUG: tokenPtr: malechi
DEBUG: aToken is: malechi
wordCounts.count(aToken) is: 0
DEBUG: tokenPtr: likes
DEBUG: aToken is: likes
wordCounts.count(aToken) is: 1

As you can see, my function is not working, but I do not know how to fix it.

Would anyone be kind to offer some advice?


Edited by Reverend Jim: Moved to programming forum

2 Years
Discussion Span
Last Post by Nathan_6

One way would be to replace:

if (wordCounts.count(aToken) == 0)        //the token is not in the map
    wordCounts.insert( pair<char*,unsigned>(aToken,1));



If atoken is present, in the map, the value will increment by 1. If, it isn't present it will be added with the default value(0) then incremented.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.