Convert a sentence into its equivalent mobile numeric keypad sequence in C++


In this tutorial, we will be discussing a program to convert a sentence into its equivalent mobile numeric keypad sequence.

For this we will be provided with a string of alphabetical characters. Our task is to print the numeric equivalent of the string i.e the numerical sequence of the keys to type that particular string.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//computing the numerical sequence
string calc_sequence(string arr[], string input){
   string output = "";
   //length of input string
   int n = input.length();
   for (int i=0; i<n; i++){
      //checking if space is present
      if (input[i] == ' ')
         output = output + "0";
      else {
            int position = input[i]-'A';
            output = output + arr[position];
         }
      }
      return output;
   }
   int 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 = "TUTORIALSPOINT";
   cout << calc_sequence(str, input);
   return 0;
}

Output

8888666777444255577777666444668

Updated on: 06-Jan-2020

538 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements