2016-11-30 69 views
1

我必须编写一个方法,它接受一个字符串并返回一个新的字符串,它复制所有元音并在其间放置一个“b”。 diphtong只有例外,其中“ab”应放在diphtong的前面。在字符串中重复元音并在中间添加字符串

例如:“你好”将返回“hebellobo” “听证会”将返回“habearing”

我已经尝试了我几个小时的代码,但我没有得到任何事情。 嗯,不是任何东西,但不能让它正确运行元音,并没有得到diphtongs。 这是我的代码:

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(); 
    } 

如果你把例如字“妈妈”时,输出中是“Mobotheberrrrr”,这是完全正常的,除了它出于某种原因重复了最后一个字母'r'。输入“目标”不幸输出“Gboalll”。

+1

不应该是“habearibing” – Henry

+0

你对“双元音”的解释是什么?它是“任何”两个连续的元音还是只有两个连续的元音符合某种条件? – VHS

+0

@亨利你是完全正确的,对不起:) –

回答

0

我最好的猜测是让replaceAlls链,因为你基本上是用重复和BS更换元音所以尽量像这样的东西:

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

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

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
  • 检查空和空字符串

注:使用StringBuilder仅仅是出于性能的考虑,直接使用该String旨意给相同的结果

+0

非常感谢您的帮助!我实现了新方法,并稍微改变了结构,现在它几乎完美地工作,唯一发生的问题是diphtongs,我无法与“isVowel”中的字符一起实现。 –

+0

我编辑了我的上一篇文章并发布了代码的修改版本 –