String Generation using Mask Parsing

String Generation using Mask Parsing

Post by James Arno » Wed, 01 Oct 2008 01:08:10


Hello,

I am new to C and I am trying to write a few small applications to get
some hands-on practise! I am trying to write a random string
generator, based on a masked input. For example, given the string:
"AAANN" it would return a string containing 3 alphanumeric characters
followed by 3 digits. This part I have managed:)

I would now like to add some complexity to this, such as repetitions
and grouping. For example, I'd like to have masks similar to:
"AAN*10", which would return two alphanumeric chars followed by a
sequence of 10 numeric characters. However, the characters could be
grouped, such as: "A(AN)*10", which would now return an alphanumeric
character followed by a sequence of ten alternating alphanumeric/
numeric characters.

I'm not really sure where to start with this next step as I have
minimal experience. Any pointers in the right direction, or sample
code, would be appreciated.

Thanks in advance,
James.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

String Generation using Mask Parsing

Post by Barry Schw » Sun, 05 Oct 2008 06:04:30

On Mon, 29 Sep 2008 11:08:10 -0500 (CDT), James Arnold



Which of the "N' characters did you count twice?


You could google for "regular expression". You might also look at the
comp.programming newsgroup because your question is not really about C
but about an algorithm.

--
Remove del for email
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

 
 
 

String Generation using Mask Parsing

Post by Jasen Bett » Sun, 05 Oct 2008 06:05:21


this sounds like a job for sscanf
you may find these examples useful. (you will probably need to study
the docs to understand them)

ok=sscanf(string,'%1[AN]*%d%n',&char_var,&int_var,&int_charsrecognised)
ok=sscanf(string,'(%a[AN]%*1[)]*%d%n',&char_ptr_var,&int_var,&int_charsrecognised)

it should be seen that the above can be used to parse letter groups
(and their associated multipliers) from expressions of the type you want.

they cannot however handle nested expressions like

(A(NA)*3)*2

for that you'll probably need to write a recursive parser, and deal
with the expressions one token at a time.

Bye.
Jasen
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.