How to count different words from a sentence I have entered?

Hi Guys, I'm having a problem that I can't solve. I want to count each word of a sentence that I entered.

For example if I entered "Hi My Name Is Cexus" then it would count
Hi Once
My Once
Name Once
Is Once
Cexus Once

This is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main()
{
     string sent;

     cout << "Enter your sentence: ";
     getline (cin,sent);

     cout << "You Typed In: \"" << sent << "\"" << endl;
     return 0;

}
Last edited on
Is anyone able to help me please?
closed account (Dy7SLyTq)
well idk how to do the once twice thrice thing but as to word count...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <sstream>
#include     <map>
#include  <string>

using std::istringstream;
using std::          map;
using std::       string;

void CountWords(map<string, int> &Dictionary, string &Input)
{
    istringstream Stream(Input);
    string Word;

    while(Stream >> Word) Dictionary[Word]++;
}


edit: posted wrong algorithim :p
Last edited on
the once is was just an example like i was just saying that the word only appeared once
Also i'm not sure how your program should work because i never learned it before.
closed account (Dy7SLyTq)
well you get how strings work obviuosly. so what do you not get? that sounds mean but i ask so i know what to adress specifically

the topics here:
using std::object;
<sstream>
<map>
istringstream operator>>
Basically comparing what i had so far to what you have, yours is very complicated. Are you able to like add onto my program to make it count different words?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
using namespace std;

void readdata(string [],int &);
void diffwords();          \\This is what i need help with

int main()
{

    return 0;
}

void readdata(string lines[],int &n)   \\reads in a string line by line
{
    n = 0;
    getline(cin,lines[n]);
    while(cin){
        n++;
        getline(cin,lines[n]);
    }

    return;
}

void diffwords(string lines[],int &n)    \\This is what i need help with
{



    return;
}


So after you enter a sentence, in the function diffwords would count how many words are the same and how many words that are different.
Last edited on
1
2
3
4
int count= 0;
char x[100];
while (cin >> x)
count ++;
Last edited on
closed account (Dy7SLyTq)
well i would do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include  <sstream>
#include      <map>
#include   <string>

using std::      istream;
using std::         cout;
using std::         endl;
using std::          cin;
using std::istringstream;
using std::          map;
using std::       string;

void ReadInText(istream&,         string&);
void WordCount (map<string, int>&, string&);

int main(int argc, char *argv[])
{
    string           Contents;
    map<string, int> Dictionary;

    ReadInText(cin, Contents);
    WordCount (Dictionary, Contents);

    for(auto &Counter : Dictionary)
        cout<< Counter.first <<" : "<< Counter.second << endl;
}

void ReadInText(istream &Stream, string &Contents) { getline(Stream, Contents); }

void WordCount(map<string, int> &Dictionary, string &Contents)
{
    istringstream Stream(Contents);
    string Word;

    while((Stream >> Word)) Dictionary[Word]++;
}
where would i put this?
1
2
3
4
int count= 0;
char x[100];
while (cin >> x)
count ++;
closed account (Dy7SLyTq)
you wouldnt. you would do that (or at least a variation of it) if you were writing c code or knew the size you wanted, then it would work. but you dont need it with my code
in the main() function would be OK!
because cin >> facility reads only one word at a time, so it happens to meet ur request.
and remember to add a cout << count; behind to see the output : P
If i type in one one one one, it should count one 4x but if i type in one two three four, it should count one 1x, two 1x, three 1x, and four 1x. How do i do this?
i'm not sure but maybe strcmp() function can meet your need.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int count[5]= {0};
char x[20];
while (cin >> x && strcmp(x,"q")){
if(!strcmp(x,"one"))
count[0]++;
else if(!strcmp(x,"two"))
count[1]++;
else if(!strcmp(x,"three"))
count[2]++;
else if(!strcmp(x,"four"))
count[3]++;
else
count[4]++;
}


and you need an additional trigger to stop the cycle, like a 'q'.
Last edited on
You need a container to store words you've read in. As you read in words you should check to see if they are already in the container. If the word is already in the container, increase the count associated with the entry. If the word is not already in the container, put the word in the container and associate it with a count of 1.

Using std::map is a convenient way to do this, as DTSCode illustrated (although the helper functions, IMO, didn't increase readability and made the code less efficient.)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <cctype>
#include <map>

// utility function for converting a string to lower case.
std::string as_lower(const std::string& text)
{
    std::string result;

    for (auto letter : text)
        result += static_cast<char>(std::tolower(letter));

    return result;
}

int main()
{
    // a map is an associative container.  In this case a word whose type is 
    // std::string is associated with a count whose type is unsigned
    // http://www.cplusplus.com/reference/map/map/
    std::map<std::string, unsigned> words;

    // using the words["word"] notation will cause an entry to be created for
    // "word" if one doesn't already exist, with a count of 0.  A reference to 
    // an existing count will be returned by the call.
    // http://www.cplusplus.com/reference/map/map/operator%5B%5D/

    std::string word;
    while (std::cin >> word)        // while a word is succesfully extracted,
        words[as_lower(word)]++;    // increase the count associated with it.


    // print the words and associated counts, w here will refer to a std::pair
    // where the first member refers to the key which is the word and the second
    // member refers to the data which is the count in this case.
    // http://www.cplusplus.com/reference/utility/pair/
    for (auto& w : words)
        std::cout << '"' << w.first << "\": " << w.second << '\n';
}


http://ideone.com/GLINwH
Topic archived. No new replies allowed.