generate all possible strings of given length given a set of characters

Discussion in 'C Programming' started by chiara, Oct 3, 2005.

  1. chiara

    chiara Guest

    Hi everybody!

    I am just at the beginning as a programmer, so maybe this is a stupid
    question...Anyway,I need to write a function in C to generate generate
    all possible strings of given length given a set of characters
    (allowing repetitions of the same character)

    For example given the characters 'E' and 'H' and maximum length 3 the
    function should generate the sequences

    H
    E
    HH
    EE
    HE
    EH
    HHH
    EEE
    HEE
    EHE
    EEH
    HHE
    HEH
    EHH

    Anybody can help?
    chiara, Oct 3, 2005
    #1
    1. Advertising

  2. "chiara" <> wrote in message
    news:...
    ....
    > For example given the characters 'E' and 'H' and maximum length 3 the
    > function should generate the sequences
    >
    > H
    > E
    > HH
    > EE
    > HE
    > EH
    > HHH
    > EEE
    > HEE
    > EHE
    > EEH
    > HHE
    > HEH
    > EHH


    This has as much to do with C as with any other language. What you need is
    task solving skills, but this group is not about solving various logic tasks
    and assignments/homeworks (as the above seems to me) but about the C
    programming language, any trouble using and misusing it. For us to help you
    out you should bring us the algorithm for the above problem (precisely
    defined either in some pseudo code or in plain English) and the C code that
    you tried to express the algorithm in but for some reason encountered a
    problem in doing so (e.g. compilation error, program behavior, etc).

    Alex
    Alexei A. Frounze, Oct 3, 2005
    #2
    1. Advertising

  3. chiara <> wrote:

    > I am just at the beginning as a programmer, so maybe this is a stupid
    > question...Anyway,I need to write a function in C to generate generate
    > all possible strings of given length given a set of characters
    > (allowing repetitions of the same character)


    Among other things, this group does not supply homework solutions. If
    you try to implement a solution in C and you need help with it, you
    can get that here. The initial effort, however, must come from you.
    Give it a shot and get back to us.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Oct 3, 2005
    #3
  4. In article <>,
    chiara <> wrote:
    >
    >
    > Hi everybody!
    >
    > I am just at the beginning as a programmer, so maybe this is a stupid
    > question...Anyway,I need to write a function in C to generate generate
    > all possible strings of given length given a set of characters
    > (allowing repetitions of the same character)
    >
    > For example given the characters 'E' and 'H' and maximum length 3 the
    > function should generate the sequences
    >
    > H
    > E
    > HH
    > EE
    > HE
    > EH
    > HHH
    > EEE
    > HEE
    > EHE
    > EEH
    > HHE
    > HEH
    > EHH
    >
    > Anybody can help?
    >


    If you want to impress your instructor, return an empty
    string as one of the combinations.
    Anonymous 7843, Oct 3, 2005
    #4
  5. chiara

    Randy Howard Guest

    chiara wrote
    (in article
    <>):

    > Hi everybody!
    >
    > I am just at the beginning as a programmer, so maybe this is a stupid
    > question...


    Well, it's debatable whether or not asking for someone else to
    do your homework for you is stupid, or simply immoral. If you
    send me your professor's email address, I'll ask him/her
    directly for you.

    > Anyway,I need to write a function in C to generate generate
    > all possible strings of given length given a set of characters
    > (allowing repetitions of the same character)


    What have you written so far? If you have the bulk of it
    written, and demonstrate that here, people will likely help you
    over the hump. If you don't try at all, you're unlikely to get
    any help at all.

    --
    Randy Howard (2reply remove FOOBAR)
    Randy Howard, Oct 3, 2005
    #5
  6. chiara

    chiara Guest

    printf problem

    Hello!

    The code I wrote is

    #include <stdio.h>
    #include <stdlib.h>
    #include <alloc.h>
    #include <math.h>
    #include <string.h>

    char** str;
    int n_seq=0;
    void generateSeq(int max_l,char* seq,int seq_len);

    void generateSeq(int max_l,char* seq,int seq_len)
    {
    int i=0,n,flag=1;

    if((seq_len>max_l))
    return;

    if(seq_len<max_l)
    {
    strcat(seq,"H");
    seq_len++;

    for(i=0;i<n_seq;i++)
    flag=strcmp(str[1],seq);

    if((flag!=0)&&(seq_len<=max_l))
    {
    strcpy(str[n_seq],seq);
    n_seq++;
    }

    generateSeq(max_l,seq,seq_len);

    seq[--seq_len]='\0';


    strcat(seq,"E");
    seq_len++;
    for(i=0;i<n_seq;i++)
    flag=strcmp(str[1],seq);

    if((flag!=0)&&(seq_len<=max_l))
    {
    strcpy(str[n_seq],seq);
    n_seq++;
    }
    generateSeq(max_l,seq,seq_len);
    //seq[--seq_len]='\0';
    }
    }

    void main (void)
    {
    int n=0,h,max_l,n_seq=0,j,k;
    char* seq="\0";

    max_l=3;

    for(h=1;h<=max_l;h++)
    n+=pow(2,h);
    str=(char **) malloc(n*sizeof(char*));
    for(k=0;k<n;k++)
    str[k]=(char *) malloc(max_l*sizeof(char*));


    generateSeq(max_l,seq,0);

    for(j=0;j<n;j++)
    {
    printf(str[j]);
    printf("\n");
    }

    scanf("%d\n",&h);

    }

    Now I have noticed that the code is actually working, the only problem
    is in the printing of the results! The strings are correctly generated
    and stored in the memory, but whenever I try to print an escape
    character or an integer number the program displays an'E'.Anybody knows
    what could be the problem and can suggest me any solution?

    Thank you in advance,

    Chiara

    chiara wrote:
    > Hi everybody!
    >
    > I am just at the beginning as a programmer, so maybe this is a stupid
    > question...Anyway,I need to write a function in C to generate generate
    > all possible strings of given length given a set of characters
    > (allowing repetitions of the same character)
    >
    > For example given the characters 'E' and 'H' and maximum length 3 the
    > function should generate the sequences
    >
    > H
    > E
    > HH
    > EE
    > HE
    > EH
    > HHH
    > EEE
    > HEE
    > EHE
    > EEH
    > HHE
    > HEH
    > EHH
    >
    > Anybody can help?
    chiara, Oct 6, 2005
    #6
  7. Re: printf problem

    On 5 Oct 2005 17:02:22 -0700, "chiara" <> wrote:

    >Hello!
    >
    >The code I wrote is
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >#include <alloc.h>
    >#include <math.h>
    >#include <string.h>
    >
    >char** str;
    >int n_seq=0;
    >void generateSeq(int max_l,char* seq,int seq_len);
    >
    >void generateSeq(int max_l,char* seq,int seq_len)
    >{
    > int i=0,n,flag=1;
    >
    > if((seq_len>max_l))
    > return;
    >
    > if(seq_len<max_l)
    > {
    > strcat(seq,"H");


    You attempt to modify the non-modifiable string literal you received
    from main. This invokes undefined behavior.

    > seq_len++;
    >
    > for(i=0;i<n_seq;i++)


    What is n_seq? There is no variable with that name defined in this
    function. The one in main is not available at this time.

    Where is your real code? Something that will compile.

    > flag=strcmp(str[1],seq);


    While str[1] does point to allocated memory, you have not put anything
    in that memory. Attempting to access uninitialized data cause
    undefined behavior.

    >
    > if((flag!=0)&&(seq_len<=max_l))
    > {
    > strcpy(str[n_seq],seq);
    > n_seq++;
    > }
    >
    > generateSeq(max_l,seq,seq_len);


    If you get this far, you will attempt to add a third char to the
    non-modifiable string literal. You are now modifying "other" constant
    data your program thinks it has access to.

    >
    > seq[--seq_len]='\0';
    >
    >
    > strcat(seq,"E");
    > seq_len++;
    > for(i=0;i<n_seq;i++)
    > flag=strcmp(str[1],seq);


    i is not used in the above statement. How many times do you need to
    perform the same comparison before you believe the result?

    Did you mean str?

    >
    > if((flag!=0)&&(seq_len<=max_l))
    > {
    > strcpy(str[n_seq],seq);
    > n_seq++;
    > }
    > generateSeq(max_l,seq,seq_len);
    > //seq[--seq_len]='\0';
    > }
    >}
    >
    >void main (void)


    int main(void) if you please.

    >{
    > int n=0,h,max_l,n_seq=0,j,k;
    > char* seq="\0";


    seq points to a non-modifiable string literal consisting of an array
    of two char.

    >
    > max_l=3;
    >
    > for(h=1;h<=max_l;h++)
    > n+=pow(2,h);
    > str=(char **) malloc(n*sizeof(char*));


    Don't cast the return from malloc. It allows the compiler to suppress
    a diagnostic if you invoke undefined behavior by omitting the
    prototype.

    > for(k=0;k<n;k++)
    > str[k]=(char *) malloc(max_l*sizeof(char*));


    You have the wrong operand for sizeof. This will cause you to
    allocate more space than you need but should not cause any problems.

    >
    >
    > generateSeq(max_l,seq,0);


    You pass the address of the non-modifiable string literal to
    generateSeq.

    >
    > for(j=0;j<n;j++)
    > {
    > printf(str[j]);
    > printf("\n");
    > }
    >
    > scanf("%d\n",&h);


    What is the intent of this.

    >
    >}
    >
    >Now I have noticed that the code is actually working, the only problem
    >is in the printing of the results! The strings are correctly generated


    Only for some strange definition of working. Your code does not
    compile cleanly and invokes undefined behavior.

    >and stored in the memory, but whenever I try to print an escape
    >character or an integer number the program displays an'E'.Anybody knows
    >what could be the problem and can suggest me any solution?
    >
    >Thank you in advance,
    >
    >Chiara
    >
    >chiara wrote:
    >> Hi everybody!
    >>
    >> I am just at the beginning as a programmer, so maybe this is a stupid
    >> question...Anyway,I need to write a function in C to generate generate
    >> all possible strings of given length given a set of characters
    >> (allowing repetitions of the same character)
    >>
    >> For example given the characters 'E' and 'H' and maximum length 3 the
    >> function should generate the sequences
    >>
    >> H
    >> E
    >> HH
    >> EE
    >> HE
    >> EH
    >> HHH
    >> EEE
    >> HEE
    >> EHE
    >> EEH
    >> HHE
    >> HEH
    >> EHH
    >>
    >> Anybody can help?



    <<Remove the del for email>>
    Barry Schwarz, Oct 6, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Girish Sahani
    Replies:
    17
    Views:
    561
    Boris Borcic
    Jun 9, 2006
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
  3. nospam
    Replies:
    2
    Views:
    310
    Barry Schwarz
    Oct 29, 2006
  4. Alex9968
    Replies:
    3
    Views:
    292
    Paul McGuire
    Apr 3, 2008
  5. OL/2
    Replies:
    2
    Views:
    123
Loading...

Share This Page