Ubbi Dubbi c#程序代码
Ubbi Dubbi是一个在单词的第一个元音之前插入字母“ub”的程序。在我的代码中,它不会在第一个元音之前做,而是在第二个元音之前做。如果我输入“hello”,那么输出是“hellubo”,而它应该是“hubello”。对不起,如果我的英语不好,我还在学习Ubbi Dubbi c#程序代码,c#,C#,Ubbi Dubbi是一个在单词的第一个元音之前插入字母“ub”的程序。在我的代码中,它不会在第一个元音之前做,而是在第二个元音之前做。如果我输入“hello”,那么输出是“hellubo”,而它应该是“hubello”。对不起,如果我的英语不好,我还在学习 Console.Write("Enter word: "); string word = Console.ReadLine(); var loc = word.IndexOfAny(new char[] {'
Console.Write("Enter word: ");
string word = Console.ReadLine();
var loc = word.IndexOfAny(new char[] {'a', 'e', 'i', 'o', 'u'});
int aloc = word.IndexOf('a');
int eloc = word.IndexOf('e');
int iloc = word.IndexOf('i');
int oloc = word.IndexOf('o');
int uloc = word.IndexOf('u');
if (aloc!= -1 && aloc > loc)
{
loc = aloc;
}
if (eloc!= -1 && eloc > loc)
{
loc = eloc;
}
if (iloc!= -1 && iloc > loc)
{
loc = iloc;
}
if (oloc!= -1 && oloc > loc)
{
loc = oloc;
}
if (uloc!= -1 && uloc > loc)
{
loc = uloc;
}
string word1 = word.Insert(loc, "ub");
Console.WriteLine(word1);
调用
IndexOfAny
后,所有工作都已完成。因此,您可以跳过大部分代码。但如果有任何元音,则应插入检查:
Console.Write("Enter word: ");
string word = Console.ReadLine();
var loc = word.IndexOfAny(new char[] { 'a', 'e', 'i', 'o', 'u' });
string word1 = loc >= 0 ? word.Insert(loc, "ub") : word;
Console.WriteLine(word1);
在代码中找到一个“e”,因此执行
loc=eloc
。但是也会找到“o”,并且在“e”检查之后执行loc=oloc
。因此,loc
的最终值是oloc
中的一个 在调用索引后
所有工作都已完成。因此,您可以跳过大部分代码。但如果有任何元音,则应插入检查:
Console.Write("Enter word: ");
string word = Console.ReadLine();
var loc = word.IndexOfAny(new char[] { 'a', 'e', 'i', 'o', 'u' });
string word1 = loc >= 0 ? word.Insert(loc, "ub") : word;
Console.WriteLine(word1);
在代码中找到一个“e”,因此执行
loc=eloc
。但是也会找到“o”,并且在“e”检查之后执行loc=oloc
。因此,loc
的最终值是oloc
中的一个 您可以通过下面的API轻松实现它
private static string processWord(string word)
{
// Vowels array
char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
//Find the index of your first vowel in your 'word'
int index = word.IndexOfAny(vowels);
//Insert 'ub' at above location
word = word.Insert(index, "ub");
//Return the word
return word;
}
或
选择任何易于理解的方法您可以通过下面的API轻松实现
private static string processWord(string word)
{
// Vowels array
char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
//Find the index of your first vowel in your 'word'
int index = word.IndexOfAny(vowels);
//Insert 'ub' at above location
word = word.Insert(index, "ub");
//Return the word
return word;
}
或
选择任何你容易理解的方法根据维基百科,“hello”应该变成“hubellubo”:。。。你说的是另一种乌比方言吗?:)@根据维基百科,“hello”应该变成“hubellubo”:。。。你说的是另一种乌比方言吗?:)@我不确定我读的是否正确,你能更好地解释这部分吗?首先你检查一下
eloc!=-1
。这是正确的,因为eloc为1(“Hello”中位置1上的“e”),所以loc=eloc
。然后检查oloc
oloc
是4,它不是-1并且大于1(oloc>loc
),因此最后loc=4
。如果你检查oloc
,你会得到正确的结果。但正如我在帖子中所说的,在索引行之后,所有其他检查都是不必要的。我不确定我读的是否正确,你能更好地解释这部分吗?首先你检查eloc!=-1
。这是正确的,因为eloc为1(“Hello”中位置1上的“e”),所以loc=eloc
。然后检查oloc
oloc
是4,它不是-1并且大于1(oloc>loc
),因此最后loc=4
。如果你检查oloc
,你会得到正确的结果。但正如我在帖子中所说,在索引行之后,所有其他检查都是不必要的。