Convert a Mobile Numeric Keypad sequence to equivalent sentence | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSentence ( string str ) { char nums [ ] [ 5 ] = { " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " } ; int i = 0 ; while ( str [ i ] != ' \0' ) { if ( str [ i ] == ' . ' ) { i ++ ; continue ; } int count = 0 ; while ( str [ i + 1 ] && 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 ( str [ i ] == ' \0' ) break ; } if ( str [ i ] == '7' str [ i ] == '9' ) { cout << nums [ str [ i ] - 48 ] [ count % 4 ] ; } else { cout << nums [ str [ i ] - 48 ] [ count % 3 ] ; } i ++ ; } } int main ( ) { string str = "234" ; printSentence ( str ) ; return 0 ; }