Answer to Question #188877 in C++ for Asad

Question #188877

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



1
Expert's answer
2021-05-10T13:41:41-0400
 #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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS