forked from swaaz/basicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram10.cpp
More file actions
48 lines (40 loc) · 860 Bytes
/
Copy pathProgram10.cpp
File metadata and controls
48 lines (40 loc) · 860 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Program 10.cpp
// Reverse words in a given string
#include <iostream>
#include<string>
using namespace std;
string reverse_string(string str) {
string temp_str = str;
int index_pos = 0;
for (int x = temp_str.length() - 1; x >= 0; x--)
{
str[index_pos] = temp_str[x];
//cout << str[index_pos];
index_pos++;
}
return str;
}
int main()
{
//taking string input
string str;
char str1[100];
cout << "String: ";
cin.getline(str1, 25);
int j = 0, begin, end, temp;
//reverse whole string
str = reverse_string(str1);
//reverse each word seperately
for (int i = 0; i < str.length(); i++) {
if (str[i] == ' ' || str[i] == '\0') {
for (begin = j, end = i - 1; begin < (i + j) / 2; begin++, end--) {
temp = str[begin];
str[begin] = str[end];
str[end] = temp;
}
j = i + 1;
}
}
//printing the words
cout << str;
}