diff --git a/challenge_1/cpp/dewie102/README.md b/challenge_1/cpp/dewie102/README.md new file mode 100644 index 000000000..843740659 --- /dev/null +++ b/challenge_1/cpp/dewie102/README.md @@ -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. diff --git a/challenge_1/cpp/dewie102/src/ReverseString.cpp b/challenge_1/cpp/dewie102/src/ReverseString.cpp new file mode 100644 index 000000000..aa995a920 --- /dev/null +++ b/challenge_1/cpp/dewie102/src/ReverseString.cpp @@ -0,0 +1,24 @@ +#include +#include + +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; +}