Skip to content

Instantly share code, notes, and snippets.

@hmenn
Created February 8, 2019 20:22
Show Gist options
  • Save hmenn/24e0dc657162c03f3cdfe15b2e1ad88d to your computer and use it in GitHub Desktop.
Save hmenn/24e0dc657162c03f3cdfe15b2e1ad88d to your computer and use it in GitHub Desktop.
reserse a string in cpp
#include <iostream>
#include <string>
std::string reverse(std::string str){
if(str.empty()){
return "";
}
int last_elem_index=str.size()-1;
return str[last_elem_index] + reverse(str.substr(0, last_elem_index));
}
int main(){
std::string str="hasan";
std::cout<<"Reversed string of '"<<str<<"' is '"<<reverse(str)<<"'"<<std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment