-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.3.cpp
More file actions
50 lines (40 loc) · 1.21 KB
/
q1.3.cpp
File metadata and controls
50 lines (40 loc) · 1.21 KB
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int get_num_spaces(string *in_string, int *string_len) {
int spaces = 0;
for (int it = 0; it < (*string_len); it++) {
if (in_string->at(it) == ' ')
spaces++;
}
return spaces;
}
void replace_spaces(string *in_string, int index, int *string_len) {
for (int it = (*string_len) - 1; it >= 0; it--) {
if (in_string->at(it) == ' ') {
in_string->operator[](index) = '0';
in_string->operator[](index - 1) = '2';
in_string->operator[](index - 2) = '%';
index -= 3;
} else {
in_string->operator[](index) = in_string->at(it);
index--;
}
}
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
string input_string;
int string_len, num_spaces;
cout << "Enter the input string: ";
getline(cin, input_string);
// cout << "entered string: " << input_string <<'\n';
cout << "enter the actual length of string: ";
cin >> string_len;
num_spaces = get_num_spaces(&input_string, &string_len);
int index;
index = string_len + num_spaces * 2 - 1;
replace_spaces(&input_string, index, &string_len);
cout << "replaced string: " << input_string << '\n';
return 0;
}