Skip to content

Instantly share code, notes, and snippets.

@arjunrao87
Last active August 29, 2015 14:11
Show Gist options
  • Save arjunrao87/538a75ca4259c715ff3d to your computer and use it in GitHub Desktop.
Save arjunrao87/538a75ca4259c715ff3d to your computer and use it in GitHub Desktop.
In-place removal of vowels from a string
public String removeVowels( String testString ){
char[] s = testString.toCharArray();
int vowelCount = 0;
int vowelStartIndex = -1;
for ( int i = 0; i < s.length; i ++ ){
if( isVowel( s[i] ) ){
if( vowelCount == 0 )
vowelStartIndex = i;
vowelCount++;
} else{
if( vowelCount != 0 ){
char temp = s[vowelStartIndex];
s[vowelStartIndex] = s[i];
s[i] = temp;
vowelStartIndex++;
}
}
}
char[] result = new char[s.length - vowelCount];
for( int i = 0; i < result.length; i ++ ){
result[i] = s[i];
}
return new String( result );
}
public boolean isVowel( char a ){
if( a == 'A' || a == 'a' ||
a == 'E' || a == 'e' ||
a == 'I' || a == 'i' ||
a == 'O' || a == 'o' ||
a == 'U' || a == 'u' ){
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment