Skip to content

Instantly share code, notes, and snippets.

@AnimeshShaw
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save AnimeshShaw/e5adec91587bb5c3f6a7 to your computer and use it in GitHub Desktop.

Select an option

Save AnimeshShaw/e5adec91587bb5c3f6a7 to your computer and use it in GitHub Desktop.
String Permutation in C++
#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