forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Reverse a String | ||
###### C++ 11 | ||
|
||
### 1. Approch to solving the problem | ||
|
||
Iterate through the string to be reveresed starting from the end and add each those letter to another string that is output. | ||
|
||
### 2. How to compile and run this | ||
|
||
In Windows - I used the Visual Studio C++ compiler, move to the proper directory and run: | ||
|
||
``` | ||
cl.exe /EHsc src/ReverseString.cpp | ||
ReverseString.exe | ||
``` | ||
|
||
### 3. How this program works | ||
|
||
I take an input string and iterate through it backwards taking each letter and adding it to the back of the output string. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
string ReverseString(string input) { | ||
string output; | ||
for(int i = input.length() - 1; i >= 0; --i) { | ||
output.push_back(input[i]); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
string input; | ||
string output; | ||
|
||
cout << "Please enter a string you wish to reverse: "; | ||
getline(cin, input); | ||
output = ReverseString(input); | ||
cout << "The reversed string is: " << output << endl; | ||
return 0; | ||
} |