Member Avatar for Nathan_6

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)];
        strcpy(aToken,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));
        else
            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?

Thanks!

Recommended Answers

All 2 Replies

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));
else
    wordCounts[aToken]+=1;

with

wordCounts[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.

Member Avatar for Nathan_6

ohh wow. That's nice.

Thank you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.