forked from raolokesh126/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_each_word.cpp
More file actions
33 lines (30 loc) · 843 Bytes
/
reverse_each_word.cpp
File metadata and controls
33 lines (30 loc) · 843 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
31
32
33
// Given a string, we need to reverse the string word wise
#include <iostream>
#include <cstring>
using namespace std;
void reverseEachWord(char input[]) {
int end,start=0;
for(int i=0;i<=strlen(input);i++)
{
if(input[i]==' ' || input[i]=='\0') // As sson as a space is enquired we need to reverse the word before the space
{
end=i-1;
while(start<end)
{
char temp=input[end];
input[end]=input[start];
input[start]=temp;
start++;
end--;
}
start=i+1; // Now we are updating our start to now point to the position after the space
}
}
}
int main() {
int size = 1e6;
char str[size];
cin.getline(str, size);
reverseEachWord(str);
cout << str;
}