I am working on a homework assignment and I need to find the longest word from a list of almost 50,000 and the reverse that word.I think the word, once reversed, should be a real word. Ex (nuts = stun, wolf = flow). The code I have right now finds the longest word, then reverses the whole list and prints out the first word it comes to in the newly reversed list.
#include <iostream>
#include <fstream>
#include <cstdlib>
std::string word;
std::string longest_word;
usingnamespace 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)
returntrue;
//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)
returntrue;
//Else word contains two or more vowels
elsereturnfalse;
}
int main()
{
ifstream inStream;
string word;
string revWord;
string::iterator it;
string longest_word;
int size;
inStream.open("words.txt", ifstream ::in);
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: " << /*revWord*/longest_word << endl;
reverse(word.begin(), word.end());
for(it=word.begin(); it!=word.end(); ++it)
revWord+=*it;
longest_word = revWord;
cout << revWord;
inStream.close( );
system("PAUSE");
return EXIT_SUCCESS;
}
I am not sure what I did wrong. Could someone please help me to see the mistake that I made?