0

Hi to all. I have to make this program where you input a text and output how many words are there and how many words with length one , two and so on to the longest word are there.I have made the done the program to count the number of words , but for the second part i have no idea. Much thanks to anyone who can help.


#include <iostream>

using namespace std;
int main()
{
int br(string);

string text;
cout<< "Enter a text: ";
getline(cin,text);

cout << "Number of words: " << br(text) << endl;
return 0;
}
int br(string text)
{
int dumi = 1;
for(int i = 0; i < text.length();i++){
    if(text[i] == ' ' || text[i] == ',' || text[i] == ';' || text[i] == '.' )
        dumi++;}
return dumi; 
}
user103220
  • 35
  • 1
  • 9

4 Answers4

1

well, I think you took the wrong approach here: you should count the number of words for each length then sum them to get the total number of words.

For the word lengths I, would declare an array, where the index in the array represents the length of the word, and increment that index in the array each time I find a word of that specific lenght (for "word1" a[4]++, since there are no words of length 0).

Now, according to wikipedia, the longest word in the English language (assuming it's English we're talking about) is ~190.000 letters long. You don't need to consider words that long (I think), unless you want to take care of the very unlikely to come up, worst case scenatio; so probably 200 is good enough here.

Back to the code:

int words[200]; // don't foreget to initialize the elements to 0
int letterCount =0;
    for(int i = 0; i < text.length();i++){
        if(text[i] == ' ' || text[i] == ',' || text[i] == ';' || text[i] == '.' )
        {
           if(letterCount > 0)
             words[letterCount]++; 
           letterCount =0; 
        }
        else{
           letterCount++; 
        }
    }

You now need to go through the array and see how many words of each length there are.

Pandrei
  • 4,843
  • 3
  • 27
  • 44
  • 1
    'Dont forget to initialize the elements to 0', you can do that like this `int words[200] = { 0 };`. – john Dec 02 '13 at 09:19
  • @john `you can do that like this int words[200] = { 0 };` that is as wrong you it can be; you either do a memset or loop through all the elements, all 200 of them, and initialize them to 0 – Pandrei Dec 02 '13 at 09:24
  • 1
    Sorry @Pandrei but you are wrong, see here http://stackoverflow.com/questions/1065774/c-c-initialization-of-a-normal-array-with-one-default-value or here http://www.fredosaurus.com/notes-cpp/arrayptr/array-initialization.html for instance (many others too). BTW I liked your answer (nice and simple) and voted for it. – john Dec 02 '13 at 09:30
  • @john nice trick - I had no idea this is part of the standard; I looked it up and you are right; thanks for that! – Pandrei Dec 02 '13 at 09:40
  • Instead of an array for the word counter, you could use (a) a vector, and check/resize it before entering a word. Or, (b) you could use a map then you really wouldnt have to worry about maximum word size. – RichardPlunkett Dec 02 '13 at 09:50
  • This last bit is important, cause if this is a homework thing, there may be a nasty last word in the tests. – RichardPlunkett Dec 02 '13 at 09:56
  • well, assuming that a word represents and actual English word you can look up in a dictionary and not a number of characters added together, then 200 is good enough; In case you define the words as a series of chars separated by characters called "word separators" than the vector, of map solution would work better. But I like the simplicity of my initial approach. – Pandrei Dec 02 '13 at 10:03
0

You need to split the input string somehow and measure the length of each substring. See https://stackoverflow.com/posts/236234/edit for some pointers on how to split (tokenize) the input.

(It's of course possible to get the lengths of the substrings without actually splitting the input, but I think that would make things unnecessarily complicated.)

Community
  • 1
  • 1
jannep
  • 1
  • 2
  • there's actually no need to split into substrings; You can process it on the fly. You save memory and it's easier to code. – Pandrei Dec 02 '13 at 09:05
0

initialize dumi=0 because when this run and true for any if choice it will become 2 or so not from start. like if it become true for once then dumi will display 2 not 1.

Artemis
  • 141
  • 4
  • 10
0

Pandrei's answer is good, but whiel you are doing htat, I would consider improving your boundary test.

    if(text[i] == ' ' || text[i] == ',' || text[i] == ';' || text[i] == '.' )

Might do better as:

#include <cctype>
    ...
if (std::isspace(text)) || std::ispunct(text[i])) 
RichardPlunkett
  • 2,998
  • 14
  • 14