From 519c25452d46a156f2947e783a40287c9a50aa39 Mon Sep 17 00:00:00 2001 From: Tushar <98016057+tushar453@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:32:57 +0530 Subject: [PATCH] Create ReverseString.cpp Reverse String Program #94 --- ReverseString.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ReverseString.cpp diff --git a/ReverseString.cpp b/ReverseString.cpp new file mode 100644 index 0000000..27f6e3a --- /dev/null +++ b/ReverseString.cpp @@ -0,0 +1,29 @@ +#include +#include + +void reverseString(std::string& str) { + int left = 0; + int right = str.length() - 1; + + while (left < right) { + std::swap(str[left], str[right]); + left++; + right--; + } +} + +int main() { + std::string inputString; + + // Ask the user to input a string + std::cout << "Enter a string: "; + std::cin >> inputString; + + // Reverse the string in-place + reverseString(inputString); + + // Display the reversed string + std::cout << "Reversed string: " << inputString << std::endl; + + return 0; +}