-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversethestring.cpp
More file actions
44 lines (37 loc) · 886 Bytes
/
Reversethestring.cpp
File metadata and controls
44 lines (37 loc) · 886 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
/*Question:
Reverse the String
Asked in:
Qualcomm
Amazon
Microsoft
Cisco
Facebook
Given an input string, reverse the string word by word.
Example:
Given s = "the sky is blue",
return "blue is sky the".
A sequence of non-space characters constitutes a word.
Your reversed string should not contain leading or trailing spaces, even if it is present in the input string.
If there are multiple spaces between words, reduce them to a single space in the reversed string.
Seen this question in a real interview before*/
void Solution::reverseWords(string &A) {
string result="";
stack <string> s;
for(int i=0;i<A.size();i++)
{
if(A[i]==' '){
s.push(result);
result.clear();
}
else
result+=A[i];
}
A.clear();
while(!s.empty())
{
A+=s.top();
A+=" ";
s.pop();
}
return A;
}