-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverse.cpp
More file actions
30 lines (27 loc) · 823 Bytes
/
reverse.cpp
File metadata and controls
30 lines (27 loc) · 823 Bytes
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
#include <iostream>
using namespace std;
void reverseEachWord(char input[]);;
int main() {
char input[1000];
cin.getline(input, 1000);
reverseEachWord(input);
cout << input << endl;
return 0;
}
void reverseEachWord(char input[]) {
int start = 0;
int i = 0;
while (input[i] != '\0') {
if (input[i+1] == ' ' || input[i+1] == '\0') {
// cout << "at index " << i << " next char is '" << input[i+1] << "'" << endl;
for (int j = i; start < j; start++, j--) {
char temp = input[start];
// cout << "i: " << start << " j: " << j << " switching " << temp << " with " << input[j] << endl;
input[start] = input[j];
input[j] = temp;
}
start = i + 2;
}
i++;
}
}