Answer to Question #27130 in C++ for sunday

Question #27130
b. Create a function in the StringProcessing class called wordSizeOccurrence that reads several lines of text and prints a table indicating the number of one-letter words, two-letter words, three-letter words, etc. appearing in the text. For example, the phrase
1
Expert's answer
2013-04-03T10:56:47-0400
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class StringProcessing{
static const int nMax = 100;
public:
void wordSizeOccurrence(void){
stringstream _s; //stream class to operate on strings (use a string buffer)
string _word, _str;
int wordSize[nMax] = {}; //array filled with 0
getline(std::cin, _str); //extracts characters from std::cin and stores them into _str
_s<<_str; //overwrites the contents _str in _s
while(_s>>_word)
wordSize[_word.length()]++; //increments the array element that meets a given length of words
for(int i = 0; i<nMax; i++) //if there are words with length from 1 to 100 characters, print the number of theirs
if(wordSize[i]!=0) cout<<i<<"-letter words: "<<wordSize[i]<<endl;
}
};

int main(){
StringProcessing ob;
ob.wordSizeOccurrence();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
23.04.15, 18:12

Dear Obed, You're welcome. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!

Obed
23.04.15, 08:01

This was so helpful, thank you guys so much! i really appreciate your help! :D

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS