-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcp29.cpp
65 lines (58 loc) · 1004 Bytes
/
dcp29.cpp
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
60
61
62
63
64
65
#include<iostream>
#include<sstream>
using namespace std;
void encoding (string str) {
int tmp = 1;
for ( int i= 0; i < str.size()-1; i++) {
if(str.substr(i,1) == str.substr(i+1,1)) {
tmp++;
}
else
{
if (tmp == 1)
cout << "1" << str.substr(i,1);
else
{
cout << tmp << str.substr(i,1);
tmp = 1;
}
}
}
}
bool isNumber(string s)
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}
int convert(string str) {
stringstream geek(str);
int x = 0;
geek >> x;
return x;
}
void decoding (string str) {
for (int i = 0; i < str.size()-1; i++) {
if (isNumber(str.substr(i,1))) {
string ch = str.substr(i+1,1);
int x = convert(str.substr(i,1));
while(x-- > 0)
cout << ch;
}
}
}
int main()
{
string str;
cin >> str;
char ch;
cout << "e-> encoding | d-> decoding";
cin >> ch;
if (ch == 'e')
encoding(str);
else if( ch == 'd')
decoding(str);
else
exit(0);
}