Reputation: 13
I Have a String S= "I love bangalore " It should print the letters between 2 vowels like below : 1.v 2.ng 3.l 4.r
Note : I am able to print if there only 1 letter b/w 2 vowels not more than that.
This what I have tried :
String a = "i love bangalore";
String[] words = a.split(" ");
for (String word : words) {
for (int i = 1; i < word.length(); i++) {
if (word.length() > 3) {
if(i==word.length()-1){
System.out.println("skip");
}
else if(checkIsVowel(word.charAt(i))&&!checkIsVowel(word.charAt(i+1))&&checkIsVowel(word.charAt(i+2))){
System.out.println(word.charAt(i+1));
}
}
}
}
Upvotes: 1
Views: 269
Reputation: 40034
The best way to do this is as follows:
result
to non-vowel.result
result
Make certain you use print statements to help debug your program.
Upvotes: 0
Reputation: 1472
The way you are trying is not right because
Here is one way to solve it
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;
}
}
}
Input:
i love bangalore
Output:
v
ng
l
r
Upvotes: 1