forked from Hrudhay-H/Cpp_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_344.cpp
More file actions
32 lines (26 loc) · 981 Bytes
/
Copy pathLeetcode_344.cpp
File metadata and controls
32 lines (26 loc) · 981 Bytes
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
#include <bits/stdc++.h> // Includes all standard C++ libraries
using namespace std;
// Class Solution contains the function to reverse a string
class Solution {
public:
// Function to reverse a character array in place
void reverseString(vector<char>& s) {
int st = 0; // Start index
int end = s.size() - 1; // End index
// Swap characters until the middle of the array is reached
while (st < end) {
swap(s[st++], s[end--]); // Swap and move indices inward
}
}
};
int main() {
// Input character array
vector<char> s = {'h', 'e', 'l', 'l', 'o'};
Solution obj; // Creating an object of the Solution class
obj.reverseString(s); // Calling the function to reverse the array
// Printing the reversed array
for(char c : s) {
cout << c << " ";
}
return 0; // Indicating successful program execution
}