Java 在字符串中复制一个元音,并在两者之间添加一个字符串

Java 在字符串中复制一个元音,并在两者之间添加一个字符串,java,string,char,substring,Java,String,Char,Substring,我必须编写一个方法,该方法接收一个字符串并返回一个新字符串,该字符串复制所有元音,并在两者之间放置一个“b”。唯一的例外是双钳,其中“ab”应放在双钳前面 例如:“hello”将返回“hebellobo” “听证”将返回“哈伯” 我已经用我的代码做了几个小时的实验,但我什么都没做。 嗯,什么都没有,但是不能正确地表达元音,也不能表达双元音。 这是我的密码: static Scanner sc = new Scanner(System.in); public static void main(S

我必须编写一个方法,该方法接收一个字符串并返回一个新字符串,该字符串复制所有元音,并在两者之间放置一个“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);
 }
静态扫描仪sc=新扫描仪(System.in);
公共静态void main(字符串[]args)
{
System.out.print(“输入字符串:”);
字符串s=sc.nextLine();
字符串originalString=s;
对于(int i=0;i
谢谢你的帮助

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

public静态布尔元音(charc){
//TODO练习1任务b)第1部分
如果(c='a'| c='a'| c='e'| c='e'| c='e'| c='i'| c='i'| c='o'| c='o'
||c==‘|’| c==‘u’| c==‘u’| c==‘u’| c==‘Ü’){
返回true;
}否则{
返回false;
}
}
公共静态字符串toB(字符串文本){
//TODO练习1任务b)第2部分
StringBuilder b=新的StringBuilder();
对于(int i=0;i

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

我的最佳猜测是创建一个替换all链,因为您基本上是用重复的元音和bs替换元音,所以请尝试这样做:

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

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

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

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

下面是解决此问题的基本代码。 基本上,您需要检查:

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

    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
    
    String originalString=。。。
    StringBuilder b=新的StringBuilder();
    对于(int i=0;i
请将其视为一个骨骼,特别是:

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

注意:使用
StringBuilder
仅出于性能原因,直接使用
String
s将得到相同的结果

这不应该是“habearibing”吗您对
双元音的解释是什么?是“任意”两个连续元音还是只有满足特定条件的两个连续元音?@Henry你说得很对,对不起:)@VHS我对双元音的解释是以下元音“au”的组合;“ai”;“ei”;“欧盟”;“ui”。非常感谢你的帮助!!!我实现了新的方法,对结构做了一点修改,现在它几乎可以完美地工作了,唯一出现的问题是双元音,我不能和“is元音”中的字符一起实现。我编辑了我以前的帖子,发布了你的代码的修改版本
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