C# 删除并添加字符串中的字母
我想用C#解决一个问题 任务如下:C# 删除并添加字符串中的字母,c#,string,C#,String,我想用C#解决一个问题 任务如下: 如果一个单词以元音开头(a,e,i,o,u或a,e,i,o,u),请删除 将第一个字母添加到末尾,然后添加“che”。如果你有 “橙色”一词翻译成“兰奇” 如果一个单词以辅音(即不是元音)开头,则在单词末尾附加“che”。例如,“鸡”这个词变成了 “胆小鬼” 如果单词的字母数为偶数,则在其末尾再加一个“e” 打印翻译的句子 示例: Hello there Amy Helloche thereche myAche 输出: Hello there Amy
- 如果一个单词以元音开头(a,e,i,o,u或a,e,i,o,u),请删除 将第一个字母添加到末尾,然后添加“che”。如果你有 “橙色”一词翻译成“兰奇”
- 如果一个单词以辅音(即不是元音)开头,则在单词末尾附加“che”。例如,“鸡”这个词变成了 “胆小鬼”
- 如果单词的字母数为偶数,则在其末尾再加一个“e”李>
Hello there Amy
Helloche thereche myAche
输出:
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”
或删除单词的元音,因为这些更改导致索引移动。我只能改变第一个字。我的ifs是正确的,因为如果我取消对控制台.Writelines的注释,它们会扫描每个单词。
我只是在努力添加/删除每个单词。您能给我指一下正确的方向吗?首先,我建议您更改翻译代码,使其具有作用于单词的功能,而不是作用于整个句子。因此,foreach(输出中的字符串s)
中的代码应该移动到另一个只作用于该字符串的函数。不要试图操纵传递给它的字符串,根据列出的逻辑创建一个新的字符串。创建翻译后的字符串后,将其返回给调用者。然后调用方将根据每个返回的翻译重新构造句子。让我们从使用提取方法将初始问题拆分为较小的问题开始:
最后
string Sentence = Console.ReadLine();
Console.Write(ConvertPhrase(Sentence));
用于测试输入
"It's just a simple test (demo only): nothing more!"
会得到
t'sIchee justchee ache simplechee testchee (demochee nlyochee): nothingche morechee!
在if
s结束之前,您不得更改您的单词。您需要测试所有条件并将结果保存在其他值中,最后对单词进行更改:
foreach (string s in output)
{
letter = char.ToLower(s[0]);
bool isVowel = false;
bool isEven = false;
if (letter == 'a' || letter == 'e' || letter == 'i'
|| letter == 'o' || letter == 'u')
{
isVowel = true;
}
if (s.Length % 2 == 0)
{
isEven = true;
}
//Now you can change the word
if (isVowel)
{
//Do What you want
}
if (isEven)
{
//Do What you want
}
//Console.WriteLine(firstchar);
int currentWordLength = s.Length;
}
我会说,把逻辑分解成特定的单元,这样你就可以编写测试了
static void Main(string[] args)
{
var input = Console.ReadLine();
var inputs = input.Split(' ');
var sentence = string.Join(" ", inputs.Select(ConvertWord));
Console.Write(sentence);
Console.Read();
}
internal static string ConvertWord(string input)
{
const string che = "che";
var vowels = new List<string>
{
"a","A", "e", "E", "i", "I", "o", "O", "u", "U"
};
var firstChar = input.First();
var startsWithVowel = vowels.SingleOrDefault(a => a.Equals(firstChar));
string rule2String, output;
if (string.IsNullOrEmpty(startsWithVowel))
{
output = input + che;
}
else
{
output = input.Substring(1) + startsWithVowel + che;
}
rule2String = IsLengthEven(input)
? output + "e"
: output
;
return rule2String;
}
internal static bool IsLengthEven(string input)
{
return input.Length % 2 == 0;
}
static void Main(字符串[]args)
{
var input=Console.ReadLine();
var输入=输入分割(“”);
var语句=string.Join(“,inputs.Select(ConvertWord));
控制台。写(句子);
Console.Read();
}
内部静态字符串转换字(字符串输入)
{
常量字符串che=“che”;
var元音=新列表
{
“a”,“a”,“e”,“e”,“i”,“i”,“o”,“o”,“u”,“u”
};
var firstChar=input.First();
var STARTSWITHVOMEL=元音.SingleOrDefault(a=>a.Equals(firstChar));
字符串规则2字符串,输出;
if(string.IsNullOrEmpty(startsWith元音))
{
输出=输入+che;
}
其他的
{
输出=输入。子字符串(1)+起始元音+che;
}
rule2String=IsLengthEven(输入)
?输出+e
:输出
;
返回规则字符串;
}
内部静态布尔值为ENGTHEVEN(字符串输入)
{
返回输入。长度%2==0;
}
希望这有帮助!
PS:我没有讨论边缘情况,我建议您创建StringBuilder
对象,并将适当的string
附加到IF
条件中。请尝试使用以下代码:
string Sentence = Console.ReadLine();
string[] output = Sentence.Split(' ');
char letter;
string che = "che";
StringBuilder sb = null;
Console.WriteLine("\n");
string strFinal = "";
foreach (string s in output)
{
letter = s[0];
sb = new StringBuilder(s);
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");
string s1 = sb.Remove(0, 1).ToString();
sb.Insert(s1.Length, letter);
sb.Insert(sb.Length, che);
}
else
{
// Console.WriteLine("first char of a word is a consonant");
sb.Insert(s.Length, che);
}
if (s.Length % 2 == 0)
{
// Console.WriteLine("the word has even numbers of letters");
// sb.Insert(s.Length, "e");
sb.Insert(sb.Length, "e");
}
//Console.WriteLine(firstchar);
int currentWordLength = s.Length;
strFinal += sb + " ";
}
Console.WriteLine(strFinal);
Console.ReadKey();
使用明显的扩展方法:
public static class ExtensionMethods {
// ***
// *** int Extensions
// ***
public static bool IsEven(this int n) => n % 2 == 0;
// ***
// *** String Extensions
// ***
public static bool StartsWithOneOf(this string s, HashSet<char> starts) => starts.Contains(s[0]);
public static string Join(this IEnumerable<string> strings, string sep) => String.Join(sep, strings);
}
我建议使用String.Join和String.Format一起使用Linq。那么解决方案就很简单了:
private String convertSentence() {
var sentence = "Hello there Amy";
var vowels = "aeiou";
var che = "che";
return String.Join(" ", (
from s in sentence.Split(' ')
let moveFirstToEnd = vowels.Contains(s.ToLower()[0]) && s.Length > 1
select String.Format("{0}{1}{2}{3}"
, moveFirstToEnd ? s.Substring(1) : s
, moveFirstToEnd ? s.Substring(0, 1) : String.Empty
, che
, s.Length % 2 == 0 ? "e" : String.Empty
)
)
);
}
不需要和StringBuilder混在一起,这只是分散注意力。(除非特别指示您使用)。可以这样想:如果字符串以元音开头,则取字符串的其余部分(元音后面)并将元音添加到其中。将字符串添加到一起以形成新字符串很简单,只需使用+
:-)请注意,这不需要在原始源字符串中移动元音,只需使用我提到的部分创建一个新字符串……请注意,在所有情况下都添加“che”——因此将其作为常见情况。你是怎么对付你的?@elgonzo。非常感谢你的努力!!。我听从了你的建议。在元音下,我创建了字符串newword=s.Substring(1)+s[0]+“che”;辅音下:字符串newword=s+“che”;在每种情况下,我都检查了单词的长度是偶数还是奇数,并在每个条件中添加了if case。例如,在元音情况下,如果长度%2=0,我使用Console.Write(newword++“e”+”);else控制台。Write(newword+“”);在consant一案中,我遵循了同样的逻辑。我还不了解正则表达式和方法,我刚开始学习StringBuilder.Regex,显然这是初学者的家庭作业或学习练习?@InBetween:我很抱歉Regex
(这很简单)但这似乎是在句子中出现标点符号时提取单词的最简单方法:Split
将获得(演示
不是演示
。事实上,有些家庭作业比预期的更难。我更倾向于认为你让它变得比需要的更难。OP中绝对没有标点符号的要求,也没有考虑到作业的级别,它似乎是预期的或考虑过的输入。在你的代码中, StartWith元音
将始终为空
。此外,你为什么要让元音测试变得如此不必要地过于复杂?你为什么不想将一个简单的字符数组/列表与Contains
方法结合使用…?从这个问题中可以清楚地看出,学习基本语法是一些学校作业/作业或课程cs关于编程。虽然我相信你的答案(在技术意义上)是有效的,但它使用了一个相对复杂的Linq查询,而这个查询超出了所要求的家庭作业的范围。为什么要否决一个好的答案?即使是家庭作业,其他人也会使用Stackoverflow来寻找答案。不是
private String convertSentence() {
var sentence = "Hello there Amy";
var vowels = "aeiou";
var che = "che";
return String.Join(" ", (
from s in sentence.Split(' ')
let moveFirstToEnd = vowels.Contains(s.ToLower()[0]) && s.Length > 1
select String.Format("{0}{1}{2}{3}"
, moveFirstToEnd ? s.Substring(1) : s
, moveFirstToEnd ? s.Substring(0, 1) : String.Empty
, che
, s.Length % 2 == 0 ? "e" : String.Empty
)
)
);
}