Last active
August 29, 2015 14:11
-
-
Save arjunrao87/538a75ca4259c715ff3d to your computer and use it in GitHub Desktop.
In-place removal of vowels from a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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