0

I'm trying to find all possible sub-sequence of a string. For example "abc" in this string we will find total 8 string 2^3=8 combinations. like a, b, c, ab, ac, bc, abc '\0'. But my code is only printing all character of the string. How can I do that?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char string[1000];
    int count, i;
    gets(string);
    int len = strlen(string);
    for(i=0; i<len; i++) {
        printf("%c ", string[i]);
    }
    return 0;
}
chrk
  • 4,037
  • 2
  • 39
  • 47
user3673621
  • 11
  • 1
  • 1
  • What part of your code, in your opinion, would find these subsequences? Do you know how this code works? – vgru May 25 '14 at 12:30
  • What you are asking is generating all possible subsets(power set) – coder hacker May 25 '14 at 12:30
  • Do you want just to count (use math instead) or print all combinations? – NirMH May 25 '14 at 12:30
  • Yes I wanna to find the power set of the string. – user3673621 May 25 '14 at 12:31
  • I'd rather describe the problem as "all possible subsets of characters in a string". Then, does upper/lower case matter? What is answer for "aaa"? – hyde May 25 '14 at 12:37
  • I have searched a lot but I didn't get my ans what am wishing to do. For example in case "aba" aa won't be counted. so the result of count of this will 7. and this is "a", "ab", "aba", "aa", "b", "ba", "a" and ""(empty string). But among the subsequences all are valid except "aa". – user3673621 May 25 '14 at 12:42

2 Answers2

0

To my understanding, you need a totally different loop. You would loop over all positions i in the string; the character at each position would either be printed or not. The problem can be solved with recursion, which pehaps is easier than with an iterative approach.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • I'm wishing to do this. Example: let, S = "aba" then possible subsequences are "a", "ab", "aba", "aa", "b", "ba", "a" and ""(empty string). But among the subsequences all are valid except "aa". if input for this aba then output will be: 7 – user3673621 May 25 '14 at 12:57
  • I don't quite understand why "aa" would be an invalid subsequence; is that because adjacent positions are required to be different, as you write in the comment above? In your example, the subsequence "a" appears twice; should both occurences of "a" be considered different because "a" can be obtained by deletion in different ways? – Codor May 25 '14 at 17:31
0

What you can do is consider string as a set and use following algorithm to print all subsets

#include <stdio.h>
#include <string.h>

int include[1000];
int n;
char s[1000];
int count=1;
void subsets(int i)
{   int j;
    if(i==n){
        int f=0;

        char temp[1000]="";
        int cnt=0;
        for(j=0;j<n;++j){

            if(include[j]){

                temp[cnt]=s[j];

                if(cnt>0 && temp[cnt]==temp[cnt-1]){ f=1; break;}
                ++cnt;

            }
        }
        temp[cnt]='\0';
        if(!f){ printf("%d =",count); printf("%s\n",temp);  
        ++count;}

     //printf("\n");
   }
    else{
        include[i] = 1;      // This element will be in the subset
        subsets(i + 1);
        include[i] = 0;     // This element won't be in the subset
        subsets(i + 1);
  }
}





void main(){

    scanf("%s",s);
   // printf("%s",s);
    n=strlen(s);
    subsets(0);

}
coder hacker
  • 4,819
  • 1
  • 25
  • 50
  • Thank you for your ans. But Please try to implement this in C programming. Cause am a new programmer. – user3673621 May 25 '14 at 12:46
  • I'm wishing to do this. Example: let, S = "aba" then possible subsequences are "a", "ab", "aba", "aa", "b", "ba", "a" and ""(empty string). But among the subsequences all are valid except "aa". if input for this aba then output will be: 7 – user3673621 May 25 '14 at 12:48
  • Subsequence of this string does not have same character at adjacent positions. Subsequence of string S can be obtained from deleting zero or more characters from string S. Thank you in advance. – user3673621 May 25 '14 at 12:52
  • No. I'm wishing to do this willingly. Thank you for your comment. – user3673621 May 25 '14 at 12:55
  • Subsequence of this string does not have same character at adjacent positions. Subsequence of string S can be obtained from deleting zero or more characters from string S. Thank you in advance. – user3673621 May 25 '14 at 12:58
  • Did the updated answer help? Or you are trying to achieve something else? – coder hacker May 25 '14 at 13:19