c# - 删除和添加字符串中的字母

标签 c# string

我正在尝试用 C# 解决一个问题。

这是任务:

  • If a word begins with a vowel (a, e, i, o, u or A, E, I, O, U), remove the first letter and append it to the end, then add "che". If you have the word “orange” It translates to “rangeoche”
  • If a word begins with a consonant (i.e. not a vowel), append "che" to the end of the word. For example, the word "chicken" becomes "chickenche".
  • If the word has even number of letters append one more "e" to the end of it.

Print the translated sentence.

示例:

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 是正确的,因为如果我取消注释 Console.Writelines 它们会扫描每个单词。 我只是在努力添加/删除每个单词。你能给我指出正确的方向吗?

最佳答案

我建议您创建 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();

关于c# - 删除和添加字符串中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53419325/

相关文章:

c# - WPF DataGrid - 将数据项的属性绑定(bind)为 CommandParameter

C# RegEx 同时匹配字符串开头和单词开头

c# - 使用 Linq 查找项目的值列表

c - 使用atoi检查字符串

python - "TypeError: string argument without an encoding",但字符串已编码?

java - Java 中的字符串拆分未按预期工作,给出了错误的结果

c# - Linq 查询- 字典中的集合

c# - 格式 DropDownList.TextValue

javascript - 大括号在变量声明中是什么意思?

c++ - 模板和字符串转换