Skip to content

Instantly share code, notes, and snippets.

@priyadarshitathagat
Created September 30, 2016 15:48
Show Gist options
  • Save priyadarshitathagat/5def360e1712bf0925b4cb5cbdd2eedc to your computer and use it in GitHub Desktop.
Save priyadarshitathagat/5def360e1712bf0925b4cb5cbdd2eedc to your computer and use it in GitHub Desktop.
Combination of Strings
#include <iostream>
#include <cstring>
#include<cstdio>
using namespace std;
void swap(char*,char*);
void combo(char *, int, int);
void combo(char*);
int main()
{
char ch[100];
cout<<"Enter a string: ";
cin.getline(ch,100);
cout<<endl<<"The possible combinations are: "<<endl;
combo(ch);
return 0;
}
void swap(char* a, char* b)
{
char temp = *a;
*a = *b;
*b = temp;
}
void combo(char *word, int n, int l)
{
if (n == l - 1)
cout << word << endl;
else
{
for (int i = n; i < l; i++)
{
swap((word+n), (word+i));
combo(word, n + 1, l);
swap((word+n), (word+i));
}
}
}
void combo(char *p)
{ int l=strlen(p); char *str; str=new char[l+1]; strcpy(str,p);
l = strlen(str);
int pos = 0;
combo(str, pos, l);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment