-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseAString.cpp
62 lines (44 loc) · 1.4 KB
/
reverseAString.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
56
57
58
59
60
61
62
#include <iostream>
#include <string>
using namespace std;
/*
Question: Reverse a string words
example: The house blue
result: blue house The
Lessons learned: Always check for edge cases, in this question, at first I didn't check when
the loop gets to the end my condition if (str[m] ==' ') fails and you
don't reverse the last word
*/
void reverseString(string &str){
int len = str.length();
int limit = len / 2;
// --> Reversing the characters in the whole string
for(int i = 0; i < limit; i++){
char holder = str[i];
str[i] = str[len - i -1];
str[len-i-1] = holder;
}
//--> start of the word to be reversed
int start = 0;
for (int i =0; i < len; i++){
if(str[i] == ' ' || i+1 == len){ //--> when you hit a white space or end of string
if(i+1 == len) i++;
int length = i - start;
int limit = length / 2;
for(int m = 0; m < limit; m++){ // --> reverse just the word
char holder = str [start + m];
str[start + m] = str[i-1-m];
str[i-1-m] = holder;
}
start = i+1;
}
}
}
int main() {
cout<<"Hello"<<endl;
string str = "the house blue";
cout<<"String: "<<str<<endl;
reverseString(str);
cout<<"String outside: "<<str<<endl;
return 0;
}