I have a homework assignment that where I need to find the longest word that has the most amount of consectutive vowels. here is the description:

The text file words.txt
contains an alphabetically sorted list of English words. Note that the words
are in mixed upper and lowercase.

Write a program to read each word in, one line at a time, and help you find the word that has the most consecutive vowels. Only use the letters 'a', 'e', 'i', 'o', and 'u' as a vowel.

For example, the word “aqueous†has four consecutive vowels. However, there is a word in the list with five consecutive vowels. What is it?

This is what I have so far"

#include <iostream>
#include <fstream>
#include <cstdlib>


std::string word; 
std::string longest_word; 
using namespace std;

bool is_single_vowel(string word){
     //all words contain at least 1 vowel; find it:
           size_t found = word.find_first_of("aeiouAEIOU");
           //Handle Speacial Case: If no vowel was detected, 
           // word must be using single 'y' as a vowel:
           
           if(found == string::npos)
           return true;
           
       //Attempt to find any occurance of a second vowel
       found = word.find_first_of("aeiouAEIOU", found +1);
       
       //If no second vowel was detected, return TRUE
       if(found == string::npos)
       return true;
       //Else word contains two or more vowels
       else
       return false;
}



int main()
{
 ifstream inStream;
 
 string  word;
 string longest_word;
int size;

inStream.open("words.txt", ifstream ::in);




//for (int i = 0; i< 50000; i++)

while(inStream) {
       
       inStream >> word;
       

      cout << word[ 0 ];
      system("pause");

    if (word.size () > longest_word.size () && is_single_vowel(word))
    longest_word = word;
    
    
   

} 


cout << "The longest word with one vowel is: " << longest_word << endl;
 
 inStream.close( );   
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

This program finds the longest word with only one vowel. A hint that someone gave me was that I would need to have two counters, one that counts the vowels and then something that will store the word with the most vowels until a word with more vowels was found. I am not sure how to do something like that though. If someone could help me with this I would appreciate it.

Have you come up with any ideas at all?

Yes, but it did not work and my professor told me that I was wrong. I thought I should extend the "if statemnet" or stick into another loop that continously check for vowels that are right next to each other. But like I said, that did not work at all. This newest idea I'm trying is only halfway working, that why I was asking for help.

Since you didn't bother to tell us what your new idea is, we can't help you. So far you've given us nothing to help with.

Start by rewriting the assignment into short statements telling you each of the individual requirements you need to consider. For example (these may be wrong):
1) Find the longest word
2) Find word with most vowels
3) Find word with most consecutive vowels
etc.

Then expand each to describe how to find/code these requirements.

Since you didn't bother to tell us what your new idea is, we can't help you. So far you've given us nothing to help with.

Start by rewriting the assignment into short statements telling you each of the individual requirements you need to consider. For example (these may be wrong):
1) Find the longest word
2) Find word with most vowels
3) Find word with most consecutive vowels
etc.

Then expand each to describe how to find/code these requirements.

1) Find the longest word

I was given a list of almost 50,000 words. I am to find the longest word within this list. I managed to complete this portion of the assignment.

I used a while loop that found the longest word.

2) Find the longest word with only one vowel

I need to locate one of the longest words on the list that contains only one vowel. There is a number of them as long as I send one to the screen then I did what was asked.

I have this part of the assignment working as well.

I added boolean logic and some if statements. The bool found the words that contained any vowels. Then the if statement read the string to see if it had more than one vowel. If so it was returned as false, if not it was returned as true. Then I sent it to be printed to the screen.

3)Find word with most consecutive vowels

Now I need to modify the program so that it will find the longest word with the most consecutive vowels. I am expected to send the longest word with one vowel to the screen along with the longest word that has the most consecutive vowels.

Since I still need the first part of the program. The newest idea that I am trying was to keep that all the same. And in a separate function repeat the bool conditions and the while conditions. So I can eliminate words without vowels and find only the longest out of the words that have vowels. I am trying to make a function that will read each letter of the word and count the vowels that are right next to each other. Once it find a consonant it will send that word to a separate counter that will store the word until a word with more consecutive vowels are found.

I have all the same code as before now I have extended the bool to find as many vowels as possible. I am currently finding the longest words with the most vowels. I need help with reading each letter at a time so I can check to see if the vowels are next to each other.

I need help with reading each letter at a time

To access each character of a std::string one at the time, you can use the operator []

string s = "abcdefg";
  for(size_t ii = 0; ii < s.length(); ++ii)
  {
    cout << "s[ii]: " << s[ii] << endl;
  }

>> I am trying to make a function that will read each letter of the word and count the vowels that are right next to each other.
Good idea.

>> Once it find a consonant it will send that word to a separate counter
Be sure to check the whole string, i.e. don't stop at encountering the first consonant.

You may need something like this:

bool is_vowel(char& test)
{
     switch(test)
     {
          case 'a':
          case 'e':
          case 'i':
          case 'o':
          case 'u':  return true;
          default :  return false;
     }
}

Edited 6 Years Ago by Clinton Portis: n/a

You may need something like this:

bool is_vowel(char& test)
{
     switch(test)
     {
          case 'a':
          case 'e':
          case 'i':
          case 'o':
          case 'u':  return true;
          default :  return false;
     }
}

When I tried doing it this way it gave me an error saying switch quantity not an interger.

This article has been dead for over six months. Start a new discussion instead.