C Programming ... Help Needed...

Status
Not open for further replies.

vinayasurya

Journeyman
Can u suggest me any way to print all the combinations of a string using C language. This is an assignment and I found no way to do that.. Plz help..
 

NikhilVerma

Padawan
Enjoy ! :D

Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void swap(int &a,int &b)
{
	int c=a;
	a=b;
	b=c;
}
void comb(char str[],int &l,int arr[],int ch,int &p,int &n)
{
	if(p<1)
		p=n-1;
	if(ch!=0)
	{
		swap(arr[p],arr[p-1]);
		for(int i=0;i<n;i++)
			cout<<str[arr[i]];
		cout<<endl;
		comb(str,l,arr,ch-1,p-1,n);
	}
}
void main()
{
	clrscr();
	char str[20];
	int n,l,arr[20];
	cout<<"Input a word (less than 10 letters)"<<endl;
	gets(str);
	cout<<"Enter no of letters"<<endl;
	cin>>n;
	for(int i=0;i<n;i++)
		arr[i]=i;
	l=strlen(str);
	comb(str,l,arr,n*(n-1),n-1,n);
	getch();
}


Made using recursion...
Please do credit the author that is me !
:wink:
 
Status
Not open for further replies.
Top Bottom