如何使用java打印两个元音之间的字母
我有一个字符串S=“我爱班加罗尔”它应该打印两个元音之间的字母,如下所示: 1.v 2.ng 3.1 4.r 注意:如果只有1个字母b/w,2个元音不超过此值,我可以打印 这就是我所尝试的:如何使用java打印两个元音之间的字母,java,Java,我有一个字符串S=“我爱班加罗尔”它应该打印两个元音之间的字母,如下所示: 1.v 2.ng 3.1 4.r 注意:如果只有1个字母b/w,2个元音不超过此值,我可以打印 这就是我所尝试的: String a=“我爱班加罗尔”; String[]words=a.split(“”); for(字符串字:字){ for(int i=1;i3){ if(i==word.length()-1){ System.out.println(“跳过”); } else if(checkis元音(word.cha
String a=“我爱班加罗尔”;
String[]words=a.split(“”);
for(字符串字:字){
for(int i=1;i3){
if(i==word.length()-1){
System.out.println(“跳过”);
}
else if(checkis元音(word.charAt(i))&&!checkis元音(word.charAt(i+1))&&checkis元音(word.charAt(i+2))){
System.out.println(word.charAt(i+1));
}
}
}
}
您尝试的方式不正确,因为
String[] words = str.split(" ");
for (String word : words) {
int firstVowelIndex = -1;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (checkIsVowel(ch)) {
// if vowel index is found again and there exists at least one character between the two vowels
if (firstVowelIndex != -1 && i - firstVowelIndex != 0) {
System.out.println(word.substring(firstVowelIndex + 1, i));
}
// vowel index is assigned
firstVowelIndex = i;
}
}
}
输出:
v
ng
l
r
最好的方法如下:
result
设置为非元音result
result
v
ng
l
r