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 = 0; i < input.length(); i++)
{
if (input[i] == ' ')
ans += '0';
else
{
int idx = input[i] - 'A';
ans += s[idx];
}
}
cout << ans << endl;
return 0;
}
Thank You