-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseWordWise.cpp
59 lines (54 loc) · 1.1 KB
/
ReverseWordWise.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
#include <iostream>
using namespace std;
int getLength(char input[])
{
int length = 0;
for (int i = 0; input[i] != '\0'; i++)
{
length++;
}
return length;
}
void reverseStringWordWise(char input[])
{
int length = getLength(input);
int i = 0;
int j = length - 1;
// Reverse the whole sentence
while (i < j)
{
char temp = input[i];
input[i] = input[j];
input[j] = temp;
i++;
j--;
}
cout << input << endl;
// Reverse the character in words
int k = 0;
int start = 0;
int end = 0;
for (int k = 0; k <= length; k++)
{
if (input[k] == ' ' || input[k] == '\0')
{
end = k - 1;
while (start < end)
{
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
}
start = k + 1;
}
}
}
int main()
{
char input[1000];
cin.getline(input, 1000);
reverseStringWordWise(input);
cout << input << endl;
}