-
Notifications
You must be signed in to change notification settings - Fork 111
/
ha1.cpp
40 lines (35 loc) · 893 Bytes
/
ha1.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
#include <bits/stdc++.h>
using namespace std;
// reverses individual words of a string
void reverseWords(string str)
{
stack<char> st;
// Traverse given string and push all characters
// to stack until we see a space.
for (int i = 0; i < str.length(); ++i) {
if (str[i] != ' ')
st.push(str[i]);
// When we see a space, we print contents
// of stack.
else {
while (st.empty() == false) {
cout << st.top();
st.pop();
}
cout << " ";
}
}
// Since there may not be space after
// last word.
while (st.empty() == false) {
cout << st.top();
st.pop();
}
}
// Driver program to test function
int main()
{
string str = "Geeks for Geeks";
reverseWords(str);
return 0;
}