Skip to content

Instantly share code, notes, and snippets.

@thmain
Created May 16, 2018 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/4e51e07ea4ef4d95ac0c985e5f79498e to your computer and use it in GitHub Desktop.
Save thmain/4e51e07ea4ef4d95ac0c985e5f79498e to your computer and use it in GitHub Desktop.
public class ReplaceVowels {
public String replaceVowels(String input){
String result = "";
char last_visited_non_vowel = '\0';
for (int i = input.length()-1; i>=0 ; i--) {
char x = input.charAt(i);
if(isVowel(x)){
if(last_visited_non_vowel!='\0') {
result = String.valueOf(last_visited_non_vowel) + result;
}
}else{
last_visited_non_vowel = x;
result = String.valueOf(x) + result;
}
}
return result;
}
public boolean isVowel(char x){
if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u'
||x=='A'||x=='E'||x=='I'||x=='O'||x=='U')
return true;
else
return false;
}
public static void main(String[] args) {
ReplaceVowels r = new ReplaceVowels();
String input = "abcdefg";
System.out.println("Input: " + input + ", Output: " + r.replaceVowels(input));
input = "aaaabccceee";
System.out.println("Input: " + input + ", Output: " + r.replaceVowels(input));
input = "aaaa";
System.out.println("Input: " + input + ", Output: " + r.replaceVowels(input));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment