Replace every consonant sequence with its length in the given string | Java implementation of the approach ; Function to return the converted string after replacing every consonant sequence with its length ; To store the resultant string ; Checking each character for consonant sequence ; Count the length of consonants sequence ; Add the length in the string ; Add the vowel ; Check for the last consonant sequence in the string ; Return the resultant string ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static String replaceConsonants ( String str ) { String res = " " ; int i = 0 , count = 0 ; while ( i < str . length ( ) ) { if ( str . charAt ( i ) != ' a ' && str . charAt ( i ) != ' e ' && str . charAt ( i ) != ' i ' && str . charAt ( i ) != ' o ' && str . charAt ( i ) != ' u ' ) { i ++ ; count ++ ; } else { if ( count > 0 ) res += count ; res += str . charAt ( i ) ; i ++ ; count = 0 ; } } if ( count > 0 ) res += count ; return res ; } public static void main ( String [ ] args ) { String str = " abcdeiop " ; System . out . println ( replaceConsonants ( str ) ) ; } }