#include <iostream>
using namespace std;
char firstLetter;
int pigLatin();
string word;
int wordFinder();
int firstVowel;
int x;
char vowel = ('a' && 'e' && 'i' && 'o' && 'u' && 'y' && 'A' && 'E' && 'I' && 'O' && 'U' && 'Y');
string engSentence;
char* letter = &engSentence[0];
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 pigLatin()
{
int lastLetter = word.length();
firstLetter = word[0];
if (vowelChecker(firstLetter)) //if the first letter of a word is a vowel...
{
cout << word << "way "; // print the word then way
}
else //if the first letter is not a vowel...
{
for (x = 1; x < lastLetter; x++) //in the loop of starting at the second letter and going to the last letter...
{
if (vowelChecker(x)) // check each letter to see if there is a vowel...
{
int firstVowel = x; //says that the first vowel is at point x
break;
}
}
string firstPortion = word.substr(0, firstVowel);
string secondPortion = word.substr(firstVowel, lastLetter);
cout << secondPortion << firstPortion << "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
}
return 0;
}
int wordFinder() //will find words within a string
{
char* letter = &engSentence[0];
while ( *letter != '\0') //while the string isn't done...
{
if ( *letter == ' ' || *letter == '.' || *letter == ',' || *letter == '-' || *letter == '!' || *letter == '?') //if there is a space, comma or period...
{
pigLatin(); //run piglatin func
cout << " "; // add a space before the next word
word = ""; //sets the word back to empty
}
else
{
word += *letter; //adds letters to the word if no space comma or period is found.
}
letter++;
}
return 0;
}
int main()
{
cout << "Please enter a sentence for me to translate: " << endl;
cout << "**REMEMBER TO END SENTENCES WITH A PERIOD OR QUESTION MARK OR EXPLANATION MARK!**" << endl;
getline(cin, engSentence); //will get the whole line entered
wordFinder(); //runs wordfinder function
}
到目前为止,它只适用于元音是第一个字母的情况。 (if(vowelChecker(firstLetter)){cout&lt;&lt;&lt; word&lt;&lt;&#34; way&#34 ;;})这部分文字......其他部分似乎没有在Piglatin功能。另外,我收到一条警告,说第一个元素没有被使用,这可以解释为什么我的程序没有正常运行。不确定为什么......
答案 0 :(得分:1)
if (vowelChecker(x)) // check each letter to see if there is a vowel...
这是传递循环变量x,而不是该位置的字符。你想要的是:
if (vowelChecker(word[x])) // check each letter to see if there is a vowel...