Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store words from the sentence

I have the following C++ code that calculates the total number of words and stores the the value in count variable.
So, the question is how can I store those particular words from a sentence in a variable so I can later use them to match the words in the sentence if I pass a word to match with.

Thanks for the help.

#include <iostream>
#include <cstring>
#include <new>
#include <cctype>

int wordsInString(const char* );

int main()
{
    wordsInString("My name is Donnie");
    return 0;
}

int wordsInString(const char* s)
{
    int count = 0;
    int len = strlen(s);
    int i;
    for(i=0;i<len;i++)
    {
        while(i<len && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n'))
        {
            i++;
        }
        if(i<len)
        {
            count++;
            while(i<len && (s[i] != ' ' && s[i] != '\t' && s[i] != '\n'))
            {
                i++;
            }
        }
    }
    std::cout << "The total count: " << count << std::endl;
    return count;
}```

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Perhaps I misunderstood the problem in my initial comments, but if you just want to count the number of space-delimited "words" in a string, then I recommend using an std::istringstream for the string, then use a loop extracting the space-delimited "words" one by one using the normal input operator >> into a dummy string variable, and increase a counter in the loop.

Perhaps something like this:

unsigned wordsInString(std::string const& string)
{
    // A stream we can read words from
    std::istringstream stream(string);

    // The word counter
    unsigned word_counter;

    // A dummy string, the contents of the string will never be used
    std::string dummy;

    // While we can read words from the stream, increase counter
    for (word_counter = 0; stream >> dummy; ++word_counter)
    {
        // Empty
    }

    // Return the counter
    return word_counter;
}

This works because:

  • The result of the >> stream extract operator is the stream itself, and when converted to a boolean value it will be false when we reach the end of the stream, which breaks the loop; And

  • The stream extraction operator separates on white-space (space, newline, tab, etc.); And

  • For each successful extraction of a word from the stream, we will increase the counter, so the loop counts the "words" in the stream.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading