我正在尝试解决一个C#问题。
这是任务:
- 如果一个单词以元音字母(a、e、i、o、u或A、E、I、O、U)开头,删除第一个字母并将其附加到末尾,然后添加"che"。例如,单词“orange”翻译为“rangeoche”。
- 如果一个单词以辅音字母(即不是元音字母)开头,则将"che"附加到单词的末尾。例如,“chicken”变成了“chickenche”。
- 如果单词的长度是偶数,则在其末尾再添加一个"e"。
打印翻译后的句子。
示例:
Hello there Amy
输出:
Helloche thereche myAche
这是我目前为止所做的:
string Sentence = Console.ReadLine();
string[] output = Sentence.Split(' ');
char letter;
string che = "che";
StringBuilder sb = new StringBuilder(Sentence);
foreach (string s in output)
{
letter = s[0];
if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i'
|| letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U')
{
// Console.WriteLine("first char of the word is a vowel");
}
else
{
sb.Insert(s.Length,che);
// Console.WriteLine("first char of a word is a consonant");
}
if (s.Length % 2 == 0)
{
// Console.WriteLine("the word has even numbers of letters");
}
//Console.WriteLine(firstchar);
int currentWordLength = s.Length;
}
Console.WriteLine(sb);
问题在于我无法添加
"che"
或删除单词中的元音字母,因为这些更改会导致索引移动。我只能更改第一个单词。如果我取消注释Console.Writelines
,它们会扫描每个单词,我的条件语句是正确的。
我只是难以添加/删除每个单词。您可以指点我正确的方向吗?