forked from ravya1108/hactoberfest2023
-
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.
Reverse String Program ravya1108#94
- Loading branch information
Showing
1 changed file
with
29 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,29 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
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; | ||
} |