Last active
August 29, 2015 14:23
-
-
Save AnimeshShaw/e5adec91587bb5c3f6a7 to your computer and use it in GitHub Desktop.
String Permutation in C++
This file contains hidden or 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 <cstring> | |
| void swap(char* src, char* dst) | |
| { | |
| char ch = *dst; | |
| *dst = *src; | |
| *src = ch; | |
| } | |
| void permuteString(char* s, int beg, int end) | |
| { | |
| int i; | |
| int range = end - beg; | |
| if (range == 1) { | |
| std::cout<<s<<std::endl; | |
| } else { | |
| for(i=0; i<range; i++) { | |
| swap(&s[beg], &s[beg+i]); | |
| permuteString(s, beg+1, end); | |
| swap(&s[beg], &s[beg+i]); | |
| } | |
| } | |
| } | |
| int main() | |
| { | |
| char str[] = "GOD"; | |
| std::cout<<"The permutations are :- "<<std::endl; | |
| permuteString(str, 0, strlen(str)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment