【问题标题】:Duplicate a vowel in String and add a String in-between在字符串中复制一个元音并在中间添加一个字符串
【发布时间】:2017-04-15 01:19:42
【问题描述】:

我必须编写一个方法,它接收一个字符串并返回一个复制所有元音的新字符串,并在其间添加一个“b”。唯一的例外是双音字,“ab”应该放在双音字前面。

例如:“hello”将返回“hebellobo” “听力”将返回“轴承”

我已经用我的代码试验了几个小时,但我什么也没做。 好吧,什么都没有,但不能让它在元音上正常运行,而且根本没有到达双元音。 这是我的代码:

static Scanner sc = new Scanner(System.in);

public static void main(String[] args)
{
    System.out.print("Enter a string: ");
    String s = sc.nextLine();
    String originalString = s;

    for (int i = 0; i < s.length(); i++)
    {
        char c = s.charAt(i);

        if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O')
                || (c == 'o') || (c == 'U') || (c == 'u'))
        {

            String front = s.substring(0, i);
            String back =  s.substring(i + 1);

            s = front + c + "b" + back;
        }
    }
    System.out.println(originalString);
    System.out.println(s);
 }

感谢您的帮助!

感谢您的帮助,我现在有了以下代码(没有扫描仪):

public static boolean isVowel(char c) {
    // TODO exercise 1 task b) part 1

    if (c == 'a' || c == 'A' || c == 'Ä' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O'
            || c == 'Ö' || c == 'u' || c == 'U' || c == 'Ü') {
        return true;
    } else {
        return false;
    }
}

public static String toB(String text) {
    // TODO exercise 1 task b) part 2

    StringBuilder b = new StringBuilder();

    for (int i = 0; i < text.length() - 1; i++) {
        char current = text.charAt(i);
        char next = text.charAt(i + 1);

        if (isVowel(current)) {
            if (isVowel(next)) {
                // 1 - Is a vowel followed by a vowel
                // Prepend b
                b.append("b");
                // Write current
                b.append(current);
                // Write next
                b.append(next);
                i++; // Skip next vowel
            } else {
                // 2 - Is a vowel followed by a consonant
                b.append(current);
                b.append("b");
                b.append(current);
            }
        } else {
            // 3 - Is a consonant
            b.append(current);
}
    }
    for (int i = 0; i < text.length() - 1; i++) {
     char last = text.charAt(text.length() - 1);
     char current = text.charAt(i);
     if (isVowel(last)) {
        // Case 1
        b.append(current);
        b.append("b");
        b.append(current);

        // Case 2 is not possible for last letter
      } else {
         // Case 3
         b.append(last);
      }

    }
     // Here b.toString() is the required string
     return b.toString();
    }

例如,如果您输入单词“Mother”,则输出是“Mobotheberrrrr”,这很好,只是由于某种原因它重复了最后一个字母“r”。不幸的是,输入“Goal”会导致输出“Gboalll”。

【问题讨论】:

  • 不应该是“habearibing”
  • 你对diphthong的解释是什么?是“任意”两个连续元音还是只有满足特定条件的两个连续元音?
  • @Henry 你说的完全正确,抱歉 :)
  • @VHS 我对双元音的解释是以下元音 "au";"ai"; 的组合。 “ei”;“eu”;“ui”。

标签: java string char substring


【解决方案1】:

您需要知道当前字母和下一个字母。

在您的代码中,您只考虑当前字母。

这是解决问题的框架代码。 基本上你需要检查:

  • 如果当前字母是元音后跟元音
  • 如果当前字母是元音后跟辅音
  • 如果当前字母是辅音

    String originalString = ...
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < s.length() - 1; i++) {
        char current = s.charAt(i);
        char next = s.charAt(i + 1); 
    
        if (isVowel(current)) {
           if (isVowel(next)) {
              // 1 - Is a vowel followed by a vowel
              // Prepend b
              b.append("b");
              // Write current
              b.append(current);
              // Write next
              b.append(next);
              i++; // Skip next vowel
           } else {
              // 2 - Is a vowel followed by a consonant
              b.append(current);
              b.append("b");
              b.append(current);
           }
        } else {
            // 3 - Is a consonant
           b.append(current);
        }
     }
    
     char last = s.charAt(s.length() - 1);
     if (isVowel(last)) {
        // Case 1
        b.append(current);
        b.append("b");
        b.append(current);
    
        // Case 2 is not possible for last letter
      } else {
         // Case 3
         b.append(last);
      }
    
    
     // Here b.toString() is the required string
    

请仅将其视为骨架,尤其是:

  • 检查边界条件
  • 实现方法isVowel
  • 检查 null 和空字符串

注意:使用StringBuilder 只是出于性能原因,直接使用String 会得到相同的结果

【讨论】:

  • 非常感谢您的帮助!!!我实现了新方法并稍微改变了结构,现在它几乎可以正常工作了,唯一出现的问题是双元音,我无法与“isVowel”中的字符一起实现。
  • 我编辑了我以前的帖子并发布了您的代码的修改版本
【解决方案2】:

我最好的猜测是创建一个 replaceAlls 链,因为您基本上是用重复的元音和 bs 替换元音,所以试试这样的方法:

String original = something;
String adjusted = original.replaceAll("ea","abea").replaceAll("a","aba").replaceAll(...)...;

只需填写规则即可。请务必在检查单元音之前检查双元音,否则它们将被视为两个单元音

【讨论】:

    猜你喜欢
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 2014-11-05
    • 2016-10-08
    • 2011-04-22
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多