By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

generating strings

P: n/a
I searched with my problem but with no results :(

My question is: how can I generate string, having only simple pattern,
like, [0-3]mid[4-7]end
For example tyis pattern should reproduce strings like:

1. 0min4end
2. 0min5end
3. 0min6end
4. 0min7end

5. 1min4end
6. 1min5end
7. 1min6end
8. 1min7end

9. 2min4end
10. 2min5end
11. 2min6end
12. 2min7end

13. 3min4end
14. 3min5end
15. 3min6end
16. 3min7end

Is there a alghoritm for it, how can I begin with making this?
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Eddie wrote:
I searched with my problem but with no results :(

My question is: how can I generate string, having only simple pattern,
like, [0-3]mid[4-7]end
Usually with the help of 'sprintf' function. Your "pattern" is just
the format string you need to create.
For example tyis pattern should reproduce strings like:

1. 0min4end
2. 0min5end
3. 0min6end
4. 0min7end

5. 1min4end
6. 1min5end
7. 1min6end
8. 1min7end

9. 2min4end
10. 2min5end
11. 2min6end
12. 2min7end

13. 3min4end
14. 3min5end
15. 3min6end
16. 3min7end

Is there a alghoritm for it, how can I begin with making this?


One may say that there is an algorithm for everything, all you need
to do is to find it.

Read about 'for' loops and 'sprintf' function.

Also, read the FAQ. Especially section 5.

Victor
Jul 22 '05 #2

P: n/a
Eddie wrote:

I searched with my problem but with no results :(

My question is: how can I generate string, having only simple pattern,
like, [0-3]mid[4-7]end
For example tyis pattern should reproduce strings like:

1. 0min4end
2. 0min5end
3. 0min6end
4. 0min7end

5. 1min4end
6. 1min5end
7. 1min6end
8. 1min7end

9. 2min4end
10. 2min5end
11. 2min6end
12. 2min7end

13. 3min4end
14. 3min5end
15. 3min6end
16. 3min7end

Is there a alghoritm for it, how can I begin with making this?

--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad.at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 22 '05 #3

P: n/a
Eddie wrote:

I searched with my problem but with no results :(

My question is: how can I generate string, having only simple pattern,
like, [0-3]mid[4-7]end
For example tyis pattern should reproduce strings like:

1. 0min4end
2. 0min5end
3. 0min6end
4. 0min7end

5. 1min4end
6. 1min5end
7. 1min6end
8. 1min7end

9. 2min4end
10. 2min5end
11. 2min6end
12. 2min7end

13. 3min4end
14. 3min5end
15. 3min6end
16. 3min7end

Is there a alghoritm for it, how can I begin with making this?


I would start with dividing the pattern into 3 parts
a prefix
the repeat part
the rest up to the right end

eg. your given pattern [0-3]mid[4-7]end divides into

Prefix: empty
repeat part: [0-3]
rest: mid[4-7]end
function PrintPattern( string prefix, string pattern )

divide pattern into prefix_now, repeat part and rest
catanate the prefix_now to the passed prefix

if the repeat part is empty
print catanate( prefix, rest )
else
do a loop over the range of the repeat part
create a temporary string which consists of
the prefix and add the next character from the range

PrintPattern( temporary, rest )

end function

So this becomes a recursive algorithm. You might need to
tweak the above a little bit but basically that should be it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

P: n/a
Karl Heinz Buchegger wrote:
Eddie wrote:

I searched with my problem but with no results :(

My question is: how can I generate string, having only simple pattern,
like, [0-3]mid[4-7]end
For example tyis pattern should reproduce strings like:

1. 0min4end
2. 0min5end
3. 0min6end
4. 0min7end

5. 1min4end
6. 1min5end
7. 1min6end
8. 1min7end

9. 2min4end
10. 2min5end
11. 2min6end
12. 2min7end

13. 3min4end
14. 3min5end
15. 3min6end
16. 3min7end

Is there a alghoritm for it, how can I begin with making this?


I would start with dividing the pattern into 3 parts
a prefix
the repeat part
the rest up to the right end

eg. your given pattern [0-3]mid[4-7]end divides into

Prefix: empty
repeat part: [0-3]
rest: mid[4-7]end
function PrintPattern( string prefix, string pattern )

divide pattern into prefix_now, repeat part and rest
catanate the prefix_now to the passed prefix

if the repeat part is empty
print catanate( prefix, rest )
else
do a loop over the range of the repeat part
create a temporary string which consists of
the prefix and add the next character from the range

PrintPattern( temporary, rest )

end function

So this becomes a recursive algorithm. You might need to
tweak the above a little bit but basically that should be it.

Thanks its good for a start, I will try to write this, it seems loking
easy now.
Jul 22 '05 #5

P: n/a
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
Eddie wrote:
My question is: how can I generate string, having only simple pattern,
like, [0-3]mid[4-7]end

I would start with dividing the pattern into 3 parts
a prefix
the repeat part
the rest up to the right end

eg. your given pattern [0-3]mid[4-7]end divides into

Prefix: empty
repeat part: [0-3]
rest: mid[4-7]end
function PrintPattern( string prefix, string pattern )

divide pattern into prefix_now, repeat part and rest
catanate the prefix_now to the passed prefix

if the repeat part is empty
print catanate( prefix, rest )
else
do a loop over the range of the repeat part
create a temporary string which consists of
the prefix and add the next character from the range

PrintPattern( temporary, rest )

end function

So this becomes a recursive algorithm. You might need to
tweak the above a little bit but basically that should be it.


This is good for the general case, especially when you want the user to be
able to enter a string like "[0-3]mid[4-7]end" or anything else with any
number of repeating parts, then you generate the strings.

But there is a simpler algorithm:

for (int i=0; i<=3; ++i) {
for (int j=4; j<=7; ++j) {
char buffer[some big number];
sprintf("%dmid%d", i, j);
or instead of the above 2 lines use ostringstream and <<;
}
}
Jul 22 '05 #6

P: n/a
"Siemel Naran" <Si*********@REMOVE.att.net> wrote...
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
Eddie wrote:

My question is: how can I generate string, having only simple pattern,
like, [0-3]mid[4-7]end

I would start with dividing the pattern into 3 parts [...]


This is good for the general case, [...]
But there is a simpler algorithm:
[...]


I see somebody is just itching to do somebody else's homework, eh?
Not enough problems to solve at work, I take it... Or has it been
a long documentation-writing week with almost no coding? <g>
Jul 22 '05 #7

P: n/a
Eddie wrote:
I searched with my problem but with no results :(

My question is: how can I generate string, having only simple pattern,
like, [0-3]mid[4-7]end
For example tyis pattern should reproduce strings like:

1. 0min4end
2. 0min5end
3. 0min6end
4. 0min7end

5. 1min4end
6. 1min5end
7. 1min6end
8. 1min7end

9. 2min4end
10. 2min5end
11. 2min6end
12. 2min7end

13. 3min4end
14. 3min5end
15. 3min6end
16. 3min7end

Is there a alghoritm for it, how can I begin with making this?


To put things in perspective ( or to make things complicated a little
bit) google for 'regular expressions'. You will have a better idea of
implementing it. ( or even a considerable superset of the given problem)

--
Karthik
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.