Tuesday 24 June 2014

C++ program to generate all possible sub strings of a string


Here is a C++ program to generate all possible sub strings of a string


 SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main()
{
    char str[50];
    int tsets,lenght;
    cout<<"Enter string ";
    cin>>str;
    lenght=strlen(str);
    tsets=pow(2,lenght);
    cout<<"All possible substrings : "<<endl;
    for(int i=0;i<tsets;i++)
    {
        for(int j=0;j<=lenght;j++)
        {
            if(i&(1<<j))
                cout<<str[j];
        }
        cout<<endl;
    }
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment