#include <iostream>
#include <string>
std::string reverseString(const std::string& str) {
std::string reversedStr = "";
for (int i = str.length() - 1; i >= 0; --i) {
reversedStr += str[i];
}
return reversedStr;
}
int main() {
std::string subject = "C++ Programming";
std::string reversedSubject = reverseString(subject);
std::cout << "Original String: " << subject << std::endl;
std::cout << "Reversed String: " << reversedSubject << std::endl;
return 0;
}
reverseString
function takes a const std::string&
parameter (a reference to a constant string) and returns the reversed string. It initializes an empty string reversedStr
and then iterates through the input string str
in reverse order using a loop. Inside the loop, each character is appended to reversedStr
. Finally, the reversed string is returned.main
function, we declare a string variable subject
with the value "C++ Programming". We then call the reverseString
function passing subject
as an argument and store the result in reversedSubject
. Finally, we print both the original and reversed string using std::cout
.