Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > generate all possible strings of given length given a set of characters

Reply
Thread Tools

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

 
 
chiara
Guest
Posts: n/a
 
      10-03-2005
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?

 
Reply With Quote
 
 
 
 
Alexei A. Frounze
Guest
Posts: n/a
 
      10-03-2005
"chiara" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
....
> 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


 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-03-2005
chiara <(E-Mail Removed)> 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.
 
Reply With Quote
 
Anonymous 7843
Guest
Posts: n/a
 
      10-03-2005
In article <(E-Mail Removed). com>,
chiara <(E-Mail Removed)> 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.
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      10-03-2005
chiara wrote
(in article
<(E-Mail Removed). com>):

> 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)

 
Reply With Quote
 
chiara
Guest
Posts: n/a
 
      10-06-2005
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?


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-06-2005
On 5 Oct 2005 17:02:22 -0700, "chiara" <(E-Mail Removed)> 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[i]?

>
> 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>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generate a unique key from a set of strings Omatase@gmail.com Javascript 9 11-28-2006 12:39 AM
Structures or Array Notation for many possible strings of finite length? nospam C Programming 2 10-28-2006 11:17 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
printing all possible permutations of a set of characters OL/2 Perl Misc 2 05-26-2004 04:06 PM



Advertisments