我希望此程序中的输出是输入中每个单词的piglatin版本,但它只是为字符串中的每个字母打印“ay”
#include <iostream>
using namespace std;
char firstLetter;
int pigLatin();
string word;
int wordFinder();
char firstVowel;
char x;
char vowel = ('a' || 'e' || 'i' || 'o' || 'u' || 'y' || 'A' || 'E' || 'I' || 'O' || 'U' || 'Y');
string engSentence;
bool vowelChecker (char c) // will check to see if there is a vowel
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'Y')
{
return true;
}
else
{
return false;
}
}
int main()
{
cout << "Please enter a sentence for me to translate: " << endl;
getline(cin, engSentence); //will get the whole line entered
wordFinder(); //runs wordfinder function
}
int wordFinder() //will find words within a string
{
char* letter = &engSentence[0];
while ( *letter != '\0') //while the string isn't done...
{
if ( *letter == ' ' || '.' || ',') //if there is a space, comma or period...
{
cout << " "; // add a space after the word
word = ""; // create word
pigLatin(); //run piglatin func
}
else
{
word += *letter; //adds letters to the word if no space comma or period is found.
}
letter++;
}
return 0;
}
int pigLatin()
{
int lastLetter = word.length();
firstLetter = word[0];
if (firstLetter == vowel) //if the first letter of a word is a vowel...
{
cout << word << "way "; // print the word
}
else if (firstLetter != vowel) //if the first letter is not a vowel...
{
for (x = 1; x < word.length(); x++) //in the loop of starting at the second letter and going to the last letter...
{
if (vowelChecker(word.at(x) || word.at(x) == 'y' || word.at(x) == 'Y')) // check each letter to see if there is a vowel...
{
char firstVowel = x; //says that the first vowel is at point x
break;
}
}
cout << word.substr(firstVowel, lastLetter) << word.substr(0, firstVowel) << "ay ";
//above is stating that it will first print the part of the word from the first vowel to the last letter,
//then it will print from the first letter to the first vowel and add 'ay' to the end
}
}
此程序返回“ay”,因为输入中有许多字母。 例如,如果我的输入是“hello”,它会输出“ay ay ay ay ay” 帮助