1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
char char_to_digit(char ch)
{
if (ch == ' ')
return '-';
const int N = 8;
const int ERROR = -1;
string letters[] = { "ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV", "WXY" };
char digits[] = { '2', '3', '4', '5', '6', '7', '8', '9' };
for (size_t i = 0; i < N; i++)
if (letters[i].find(ch) != string::npos)
return digits[i];
return ERROR;
}
int main(int argc, char **argv)
{
string s("GET LOAN");
for (char ch : s)
{
char digit = char_to_digit(ch);
if (digit != -1)
cout << digit;
else
cout << "ERROR";
}
cout << "\n\n";
}
|