-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms_problem.txt
59 lines (54 loc) · 1.68 KB
/
sms_problem.txt
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
SMS Problem
0 - NULL, 1 - NULL, 2 - ABC, 3 - DEF, 4 - GHI, 5 - JKL, 6 - MON, 7 - PQRS, 8 - TUV, 9 - WXYZ, * - <Space>, # - <Break>
We must convert the numbers to text.
Eg: 22 - B, 23 - AD, 223 - BD, 22#2 - BA (# breaks the cycle), 3#33 - DE, 2222-22222#2 - 2A, 22222 - A (cycle must wrap around), 222222 - B
*/
#include <iostream>
using namespace std;
int main(){
cout<<"input a string of number"<<endl;
string str;
cin>>str;
/*Verify the input*/
for (int i=0; i<str.length(); ++i){
if (!((str[i]<='9' && str[i]>='0')||str[i]=='#'||str[i]=='*')){
cout<<"wrong input!"<<endl;
return -1;
}
}
string keypad[] = {"", "", "ABC2", "DEF3", "GHI4", "JKL5", "MON6", "PQRS7", "TUV8", "WXYZ9"}; // note: start from '2', add number at the end of array.
int i=0, j=0, n = str.length();//n is the length of the original string.
while(i<n){
if (str[i]=='#'){
i++;
continue;
}
else if (str[i]=='*'){
str[j++]=' ';
i++;
continue;
}
else{
int count = 0;
while(str[i]==str[i+1]){
if (i==n-1){
count++;
break;
}
count++;
i++;
}
str[j]=keypad[str[i]-'0'][(count)%(keypad[str[i]-'0'].length())];
j++;
i++;
}
}
cout<<"The sms is ";
for (int i=0; i<j; ++i)
cout<<str[i];
cout<<endl;
return 0;
}
REMARK:
1. The idea is pretty straightforward. In the outer while loop, you just need to see which case is in: a#, a* or a digit.