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.