Convert a Mobile Numeric Keypad sequence to equivalent sentence | java program for the above approach ; Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the string str ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void printSentence ( String S ) { String nums [ ] = { " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " } ; char str [ ] = S . toCharArray ( ) ; int i = 0 ; while ( i < str . length ) { if ( str [ i ] == ' . ' ) { i ++ ; continue ; } int count = 0 ; while ( i + 1 < str . length && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= '2' && str [ i ] <= '6' ) || ( str [ i ] == '8' ) ) ) break ; else if ( count == 3 && ( str [ i ] == '7' str [ i ] == '9' ) ) break ; count ++ ; i ++ ; if ( i == str . length ) break ; } if ( str [ i ] == '7' str [ i ] == '9' ) { System . out . print ( nums [ str [ i ] - 48 ] . charAt ( count % 4 ) ) ; } else { System . out . print ( nums [ str [ i ] - 48 ] . charAt ( count % 3 ) ) ; } i ++ ; } } public static void main ( String [ ] args ) { String str = "234" ; printSentence ( str ) ; } }