我正在尝试编写一个程序,其中将单词作为字符串提供作为输入,我必须重新排列单词,以便通过将所有元音移动到末尾来改变单词中字母的顺序,保持它们的顺序与它们在原始单词中出现的顺序相同
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string word = "application";
char[] letters = word.ToCharArray();
char x = new char { };
for (int j = 0; j < letters.Length; j++)
{
if ((letters[j] == 'a') | (letters[j] == 'e' ) | (letters[j] == 'i' ) | (letters[j] == 'o' ) | (letters[j]
== 'u'))
{
for (int i = 0; i < letters.Length - 1; i++)
{
x = letters[i];
letters[i] = letters[i + 1];
letters[i + 1] = x;
}
}
}
string s = new string(letters);
Console.WriteLine(s);
}
}
}
程序的输出是
ationaplic
但程序的预期输出是
pplctnaiaio
为什么我的代码没有产生我想要的输出?
编辑后的工作代码是
namespace VowelSort
{
class Program
{
static void Main(string[] args)
{
string word = "application";
char[] letters = word.ToCharArray();
char x = new char { };
int count = 0;
for (int j = 0; j < letters.Length - count; j++)
{
if ((letters[j] == 'a') | (letters[j] == 'e') | (letters[j] == 'i') | (letters[j] == 'o') | (letters[j] == 'u') | (letters[j] == 'A') | (letters[j] == 'E') | (letters[j] == 'I') | (letters[j] == 'O') | (letters[j] == 'U'))
{
for (int i = j; i < letters.Length - 1; i++)
{
x = letters[i];
letters[i] = letters[i + 1];
letters[i + 1] = x;
}
count++;
j--;
}
}
string s = new string(letters);
Console.WriteLine(s);
Console.WriteLine(count);
}
}
}