How to countstring input word by word at a time

Jan 12, 2014 at 4:02pm
I was given an exercise to write a program that read strings of input, after which the program display how many words it received, how many of the words received begin with vowels, consonants and others(non consonant or vowel). I know this may sound stupid but I am a beginner, everything went right until I bumped into this exercise. Please I need help on how to read string input word by word at a time and what character does it begin with.
thanks

Jan 12, 2014 at 6:01pm
1
2
3
4
std::string word;
while( std::cin>>word ){
   //...
}
Jan 12, 2014 at 11:22pm
In addition to the above:

create a string that contains all vowels. Then you can do:

1
2
3
4
5
6
7
8
9
if (isalpha(word[0])
{
    if (vowels.find_first_of(word[0]) != string::npos)
    {
        // increment vowels counter
    }
    else // increment consonant counter
}
else // increment other counter 

Last edited on Jan 13, 2014 at 12:53am
Jan 13, 2014 at 3:56am
In addition, you could determine what type of string it is by using a switch statement to determine it, and have it return an enum like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
enum LetterType{ TYPE_CONSONANT, TYPE_VOWEL, TYPE_OTHER };

StringType whatIsFirstLetter( std::string input ){
	switch( input[0] ){
	        case 'a': case 'e': case 'i': case 'o': case 'u':
                case 'A': case 'E': case 'I': case 'O': case 'U':
		return TYPE_VOWEL;

		case '.': case '/': case ',': // add remaining "other" cases
		return TYPE_OTHER;
	}
	return TYPE_CONSONANT; // if not a vowel or other, it must be a consonant
}
Last edited on Jan 13, 2014 at 4:00am
Feb 24, 2014 at 4:31am
The words you need to read strings out are in which docuemnt format, if they are contained within a word document, maybe you can use a word document reader that can be used to read and decode word strings. Or in other way, you just use a universal document reader, that is ocr recognizer, which is fully designed to read and decode text, word and font strings.

http://www.rasteredge.com/how-to/vb-net-imaging/word-reading/
http://www.rasteredge.com/how-to/vb-net-imaging/ocr-sdk/
Topic archived. No new replies allowed.