473,785 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi everybody!

I am just at the beginning as a programmer, so maybe this is a stupid
question...Anyw ay,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?

Nov 15 '05 #1
6 4068
"chiara" <ch*********@gm ail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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
Nov 15 '05 #2
chiara <ch*********@gm ail.com> wrote:
I am just at the beginning as a programmer, so maybe this is a stupid
question...Anyw ay,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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3
In article <11************ *********@g47g2 000cwa.googlegr oups.com>,
chiara <ch*********@gm ail.com> wrote:


Hi everybody!

I am just at the beginning as a programmer, so maybe this is a stupid
question...Anyw ay,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.
Nov 15 '05 #4
chiara wrote
(in article
<11************ *********@g47g2 000cwa.googlegr oups.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)

Nov 15 '05 #5
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_s eq=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*si zeof(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...Anyw ay,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?


Nov 15 '05 #6
On 5 Oct 2005 17:02:22 -0700, "chiara" <ch*********@gm ail.com> 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_s eq=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*si zeof(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...Anyw ay,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>>
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2621
by: Crirus | last post by:
Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH Thanks Crirus
2
1473
by: Chakravarthy | last post by:
Given an XML stream as below, is there any possibility to convert the same into any treditional class style with . (dots) as seperator of the nodes of the xml file... for instance bank.code.tostring() should reslut me "5070" and bank.description.tostring() should result "ICICI - Bangalore"
2
1324
by: nospam | last post by:
I have an application that processes 30 kinds of strings of that fit in an array. By processing, I mean -check the value of certain characters or substrings to be within contant bounds or a member of a list -check the value of certain characters or substrings to be within bounds defined by other characters or substrings -call a series of functions based on value of certain substrings.
3
5854
by: dohyohdohyoh | last post by:
I have a programming question to generate an ordered list of alphanumeric strings of length 4. two alphabets rest numberst, etc. EG 0000-9999 then A000-Z999 then AA00 to ZZ99 then AAA0 - ZZZ9 then AAAA - ZZZZ
3
10911
by: jshunter | last post by:
Does anyone know of a built-in function to return the maximum possible value of a given datatype? I have to return the biggest value for a smalldatetime or datetime in a view if the field is null, but can't find such a function. The closest I've come is: select datalength(cast(getdate() as smalldatetime)) ....but that only return the number of bytes, not the value itself, which is '6-6-2079 11:59'
4
1523
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
How do I efficiently determine which possible encoding(s) a given text is in? Can I use the iconv.h api somehow? Thanks in advance, Nordlöw
6
2324
by: Jimmy45 | last post by:
Hello, I am writing a "scrabble code" on Python. I have some problem with getting possible words from the given letters. For example: Given letters are: 'c', 'r', 'n', '*' Here '*' can be any letter. Possible words: 'cry', 'corn', 'run', etc. proper_word_list is a file containing all the english words. score_of_word is a function that computes the score of the word. hand is the letters that are given to the player
2
3209
by: alemo91 | last post by:
Write a recursive function to print all possible permutations of a given string. For example if the input string is “abc” then the set of permutations is: abc, acb, bac, bca, cab, cba. Hint: Generate all permutations that start with 'a',then 'b' then 'c'. To generate permutations starting with 'a', we need to find all permutations of "bc"
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.