Blog Archive

Convert a sentence into its equivalent mobile numeric keypad sequence

 Convert a sentence into its equivalent mobile numeric keypad sequence-Data Structure and Algorithms

Given a sentence in the form of a string, convert it into its equivalent mobile numeric keypad sequence


Input : STRIVE
Output : 7777877744488833
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

Solution:

// #include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // cout<<"Hello World!";
    string s[] = {"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;
    cin >> input;
    string ans = "";
    for (int i = 0i < input.length(); i++)
    {
        if (input[i] == ' ')
            ans += '0';
        else
        {
            int idx = input[i] - 'A';
            ans += s[idx];
        }
    }
    cout << ans << endl;
    return 0;
}

Thank You

data structures and algorithm-String data structure

No comments:

Post a Comment

Recent Post

Sort an array of 0s, 1s and 2s

  Sort an array of 0s, 1s and 2s- Data Structures   Sort an array of 0s, 1s and 2s- Data Structures Given an array of size N containing onl...