forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reverse_Words_in_String.cpp
55 lines (52 loc) · 1.36 KB
/
Reverse_Words_in_String.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
/**
* @author omkarlanghe
* Given a String of length S, reverse the whole string without reversing the individual words in it.
* Words are separated by dots.
*
* Input:
* The first line contains T denoting the number of testcases.
* T testcases follow. Each case contains a string S containing characters.
*
* Output:
* For each test case, in a new line, output a single line containing the reversed String.
*
* Example:
* Input:
* 2
* i.like.this.program.very.much
* pqr.mno
*
* Output:
* much.very.program.this.like.i
* mno.pqr
*/
#include <iostream>
#include <stack>
int main() {
int t;
std::cout << "Enter test cases : " << std::endl;
std::cin >> t;
while (t--) {
std::stack<std::string> str_stack;
std::string str, stack_string;
std::cout << "Enter the string : " << std::endl;
std::cin >> str;
for (int i = 0 ; i < str.size() ; i++) {
if (str[i] != '.') {
stack_string += str[i];
} else {
str_stack.push(stack_string);
str_stack.push(".");
stack_string = "";
}
}
str_stack.push(stack_string);
str = "";
while (!str_stack.empty()) {
str += str_stack.top();
str_stack.pop();
}
std::cout << str << std::endl;
}
return (0);
}