(BEGINNER TO C++) Determine the word with the most number of vowel and most number of consonants

write a program that ask for a long string and determine the word with the most number of vowel and most number of consonants


example: The quick brown fox jumps over the lazy dog
vowels: quick
consonants: brown

I need some help.
Last edited on
What exactly is the problem?
Separating string into words?
Calculating amount of vowels/consonants?
Finding maximum from a set of values?
it outputs two words: a word with the most number of vowel and a word with the most number of consonants.

it identifies the words in the sentence with the most number of vowel and a word with the most number of consonants.

What is your problem? Where do you stuck?
You can break this problem down by working from the outside in: start from the inputs and work forward. Then start with the outputs and work back. Pretty soon you'll have a design.

Okay, starting with the input, you need code that will break a line into individual words.
Starting from the output, it's clear that you'll need two functions: one takes a word and returns the number of vowels in it. The other takes a word and returns the number of consonants. Or you could do one function that does both:
void classifyWord(const std::string &word, int &vowels, int &consonants);

To find the anwers, you'll need to track the word with the most vowels, along with it's count. Also the word with the fewest vowels and its count:
1
2
3
4
5
6
struct WordAndCount {
    std::string word;
    int count;
};

WordAndCount vowelWord, consonantWord;


Now it should be clear how to put the pieces together:

1
2
3
4
5
6
7
8
9
10
read the string.
for each word in the string {
    count the vowels and consonants
    if the vowels > vowelWord.count {
       vowelWord.word = this word;
       vowelWord.count = this word's vowels
    }
    and the same logic for consonants.
}
print the results. 
I think he's stuck on the idea.

This is very much like the min/max problem, where you find the smallest/largest integer in an array.

Step 1: Get the array of words from the user. (If you know how to use a std::vector, use it instead of a C-style array.)

Step 2: Write a function that determines the number of vowels in a word. It should look (prototype) something like this:

int count_vowels( string s );

Step 3: Just like finding the maximum integer value in an array (or vector) of integer values, you must now find the string with the maximum number of vowels in the array (or vector) of strings. Remember that you'll need two additional variables (current string with max vowels, number of vowels in current string with max values) before iterating through the array/vector.

Step 4: Do the same thing, except counting consonants. There is an easy mathematical trick to get the number of consonants in a word if you know the number of vowels.

Hope this helps.
break it into smaller problem:
1. count total vowels in a user given word.
2. break a sentence into words. then apply idea of 1.
hey daffodandelion I would recomend that you start with this:
1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}
Topic archived. No new replies allowed.