Created
February 8, 2019 20:22
-
-
Save hmenn/24e0dc657162c03f3cdfe15b2e1ad88d to your computer and use it in GitHub Desktop.
reserse a string in cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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