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

C

chiara

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?
 
A

Alexei A. Frounze

....
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
 
C

Christopher Benson-Manica

chiara said:
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.
 
A

Anonymous 7843

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.
 
R

Randy Howard

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.
 
C

chiara

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
 
B

Barry Schwarz

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


<<Remove the del for email>>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top