Given a string, Your program will store half of the string reverse and store the rest of the string as it is in another string. your program will print the following as shown in the examples given below.
1. Original string: “danish” then your program should print “nadish”.
2. Original string: “zill-e-huma” then your program should print “-llize-huma”.
Note: You are not allowed to use built in functions of strings
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
string half_reverse(string &str)
{
string result = "";
for (int i = str.length() / 2-1; i >= 0; i--)
{
result+=str[i];
}
for (int i = str.length() / 2; i < str.length(); i++)
{
result += str[i];
}
return result;
}
int main()
{
//test funcion
string test="danish";
string res;
res = half_reverse(test);
cout <<res<<endl;
test = "zill-e-huma";
res = half_reverse(test);
cout << res << endl;
return 0;
}
Comments
Leave a comment