Convert a sentence into its equivalent mobile numeric keypad sequence

23 Jun, 2022
Given a sentence in the form of a string, convert it into its equivalent mobile numeric keypad sequence.  
Examples :  
Input : GEEKSFORGEEKS
Output : 4333355777733366677743333557777
For obtaining a number, we need to press a
number corresponding to that character for 
number of times equal to position of the 
character. For example, for character C, 
we press number 2 three times and accordingly.

Input : HELLO WORLD
Output : 4433555555666096667775553
 
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Follow the steps given below to convert a sentence into its equivalent mobile numeric keypad sequence.  
For each character, store the sequence which should be obtained at its respective position in an array, i.e. for Z, store 9999. For Y, store 999. For K, store 55 and so on.
For each character, subtract ASCII value of ‘A’ and obtain the position in the array pointed by that character and add the sequence stored in that array to a string.
If the character is a space, store 0
Print the overall sequence.
Below is the implementation of above method :  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to convert a// sentence into its equivalent// mobile numeric keypad sequence#include <bits/stdc++.h>using namespace std; // Function which computes the sequencestring printSequence(string arr[],                       string input){    string output = "";     // length of input string    int n = input.length();    for (int i=0; i<n; i++)    {        // Checking for space        if (input[i] == ' ')            output = output + "0";         else        {            // Calculating index for each            // character            int position = input[i]-'A';            output = output + arr[position];        }    }     // Output sequence    return output;} // Driver functionint main(){    // storing the sequence in array    string str[] = {"2","22","222",                    "3","33","333",                    "4","44","444",                    "5","55","555",                    "6","66","666",                    "7","77","777","7777",                    "8","88","888",                    "9","99","999","9999"                   };     string input = "GEEKSFORGEEKS";    cout << printSequence(str, input);    return 0;}
// Java implementation to convert a// sentence into its equivalent// mobile numeric keypad sequenceimport java.util.*; class GFG{     // Function which computes the sequence    static String printSequence(String arr[],                               String input)    {        String output = "";             // length of input string        int n = input.length();        for (int i = 0; i < n; i++)        {            // Checking for space            if (input.charAt(i) == ' ')                output = output + "0";                 else            {                // Calculating index for each                // character                int position = input.charAt(i) - 'A';                output = output + arr[position];            }        }             // Output sequence        return output;    }         // Driver Function    public static void main(String[] args)    {        // storing the sequence in array        String str[] = {"2","22","222",                        "3","33","333",                        "4","44","444",                        "5","55","555",                        "6","66","666",                        "7","77","777","7777",                        "8","88","888",                        "9","99","999","9999"                    };         String input = "GEEKSFORGEEKS";        System.out.println(printSequence(str, input));    }} // This code is contributed by Gitanjali.
# Python3 implementation to convert# a sentence into its equivalent# mobile numeric keypad sequence # Function which computes the# sequencedef printSequence(arr, input): # length of input string    n = len(input)    output = ""         for i in range(n):             # checking for space        if(input[i] == ' '):            output = output + "0"        else:            # calculating index for each            # character                    position = ord(input[i]) - ord('A')            output = output + arr[position]    # output sequence        return output     # Driver codestr = ["2", "22", "222",       "3", "33", "333",       "4", "44", "444",       "5", "55", "555",       "6", "66", "666",       "7", "77", "777", "7777",       "8", "88", "888",       "9", "99", "999", "9999" ] input = "GEEKSFORGEEKS";print( printSequence(str, input)) # This code is contributed by upendra bartwal
// C# implementation to convert a// sentence into its equivalent// mobile numeric keypad sequenceusing System; class GFG{     // Function which computes the sequence    static String printSequence(string []arr,                            string input)    {        string output = "";             // length of input string        int n = input.Length;        for (int i = 0; i < n; i++)        {            // Checking for space            if (input[i] == ' ')                output = output + "0";                 else            {                // Calculating index for each                // character                int position = input[i] - 'A';                output = output + arr[position];            }        }             // Output sequence        return output;    }         // Driver Function    public static void Main()    {        // storing the sequence in array        string []str = {"2","22","222",                        "3","33","333",                        "4","44","444",                        "5","55","555",                        "6","66","666",                        "7","77","777","7777",                        "8","88","888",                        "9","99","999","9999"                    };         string input = "GEEKSFORGEEKS";        Console.WriteLine(printSequence(str, input));    }} // This code is contributed by vt_m.
<?php// PHP implementation to convert a// sentence into its equivalent// mobile numeric keypad sequence // Function which computes the sequencefunction printSequence(&$arr, $input){    $output = "";     // length of input string    $n = strlen($input);    for ($i = 0; $i < $n; $i++)    {        // Checking for space        if ($input[$i] == ' ')            $output = $output + "0";         else        {            // Calculating index for each            // character            $position = ord($input[$i]) - ord('A');            $output = $output . $arr[$position];        }    }     // Output sequence    return $output;} // Driver Code // storing the sequence in array$str = array("2","22","222", "3","33","333",             "4","44","444", "5","55","555",             "6","66","666", "7","77","777","7777",             "8","88","888", "9","99","999","9999"); $input = "GEEKSFORGEEKS";echo printSequence($str, $input); // This code is contributed by ita_c?>
<script> // Javascript implementation to convert a// sentence into its equivalent// mobile numeric keypad sequence         // Function which computes the sequence    function printSequence(arr,input)    {        let output = "";               // length of input string        let n = input.length;        for (let i = 0; i < n; i++)        {            // Checking for space            if (input[i] == ' ')                output = output + "0".charCodeAt(0);                   else            {                // Calculating index for each                // character                let position = input[i].charCodeAt(0) - 'A'.charCodeAt(0);                output = output + arr[position];            }        }               // Output sequence        return output;    }         // Driver Function    let str = ["2", "22", "222",       "3", "33", "333",       "4", "44", "444",       "5", "55", "555",       "6", "66", "666",       "7", "77", "777", "7777",       "8", "88", "888",       "9", "99", "999", "9999" ]         let input = "GEEKSFORGEEKS";    document.write(printSequence(str, input));         // This code is contributed by avanitrachhadiya2155</script>
Output :  
4333355777733366677743333557777
Time complexity: O(n) 
Auxiliary Space: O(n), Linear complexity for the above code. 
Convert a sentence into its equivalent mobile numeric keypad sequence | GeeksforGeeks - YouTubeGeeksforGeeks530K subscribersConvert a sentence into its equivalent mobile numeric keypad sequence | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:20•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=hE4Nn7o4GFw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
ukasp
avanitrachhadiya2155
rupasriachanta421
mailaruyashaswi
Hash
Strings
Hash
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
What is Hashing | A Complete Tutorial
Hashing | Set 1 (Introduction)
Internal Working of HashMap in Java
Count pairs with given sum
Write a program to reverse an array or string
Reverse a string in Java
C++ Data Types
Write a program to print all permutations of a given string
Check for Balanced Brackets in an expression (well-formedness) using Stack