Saturday, July 23, 2011

Combination of strings

The Program prints all the different combinations of a given string.

The input is:
s-The String
n-The length of combinations.

#include <iostream>
#include <cstring>
using namespace std;

void combination(string str,int len)
{
        int start=0;
        int k=0;
        int sec=0;
        for(start=0;start<=str.length()-len;start++)
        {
                sec=start;
                k=0;
                while(sec+len<=str.length())
                {
                        k++;
                        sec=start+k;
                        cout<<str[start];
                        for(int j=0;j<len-1;j++)
                                cout<<str[sec+j];
                        cout<<" ";
                }
                cout<<endl;
        }
}


int main()
{
        string s;
        int n;
        cin>>s;
        cin>>n;
        combination(s,n);
}

No comments:

Post a Comment