-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path01_difference_in_ascii_codes.cpp
73 lines (57 loc) · 1.63 KB
/
01_difference_in_ascii_codes.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
66
67
68
69
70
71
72
73
/*
Problem Name: String - Difference in Ascii Codes
Take as input S, a string. Write a program that inserts between each pair of
characters the difference between their ascii codes and print the ans.
Input Format: String
Constraints: Length of String should be between 2 to 1000.
Output Format: String
Sample Input: acb
Sample Output: a2c-1b
Explanation: For the sample case, the Ascii code of a=97 and c=99,
the difference between c and a is 2. Similarly,
the Ascii code of b=98 and c=99 and their difference is -1.
So the ans is a2c-1b.
*/
#include <iostream>
using namespace std;
int main() {
string s;
cout << "Enter your string: ";
getline(cin, s);
cout << "Output: ";
int end = s.length()*2;
int idx = 0;
for(int step=0; step<=end-2; step++){
if(step%2 == 0){
cout << s[idx];
idx++;
}else{
cout << s[idx]-s[idx-1];
}
}
cout << endl;
return 0;
}
/*
Approach:
All we need to do is just traverse the whole string and
for every two adjacent characters just calculate the ascii among them.
We will print the characters and the difference between with characters
Required Code is given below :
#include <iostream>
#include <string>
using namespace std;
int main() {
char str[1000];
int nstr[2000];
cin>>str;
int i=1;
cout<<str[0];
while(str[i]!='\0'){
nstr[i]=int(str[i])-int(str[i-1]);
cout<<nstr[i]<<str[i];
i++;
}
return 0;
}
*/