generating strings

E

Eddie

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

Victor Bazarov

Eddie said:
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
 
K

Karl Heinz Buchegger

Eddie said:
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: (e-mail address removed) Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
 
K

Karl Heinz Buchegger

Eddie said:
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.
 
E

Eddie

Karl said:
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.
 
S

Siemel Naran

Karl Heinz Buchegger said:
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 <<;
}
}
 
V

Victor Bazarov

Siemel Naran said:
Karl Heinz Buchegger said:
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>
 
K

Karthiik Kumar

Eddie said:
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)
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top