generating strings

Discussion in 'C++' started by Eddie, Aug 25, 2004.

  1. Eddie

    Eddie Guest

    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?
    Eddie, Aug 25, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Aug 25, 2004
    #2
    1. Advertising

  3. 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: Web: www.gascad.com

    Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
    Karl Heinz Buchegger, Aug 26, 2004
    #3
  4. 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
    Karl Heinz Buchegger, Aug 26, 2004
    #4
  5. Eddie

    Eddie Guest

    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.
    Eddie, Aug 26, 2004
    #5
  6. Eddie

    Siemel Naran Guest

    "Karl Heinz Buchegger" <> 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 <<;
    }
    }
    Siemel Naran, Aug 27, 2004
    #6
  7. "Siemel Naran" <> wrote...
    > "Karl Heinz Buchegger" <> 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>
    Victor Bazarov, Aug 27, 2004
    #7
  8. 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
    Karthiik Kumar, Aug 28, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Glover
    Replies:
    0
    Views:
    258
    Ian Glover
    Aug 31, 2004
  2. Eddie

    generating strings

    Eddie, Aug 25, 2004, in forum: C Programming
    Replies:
    3
    Views:
    304
    Eric Sosman
    Aug 26, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    739
    Malcolm
    Jun 24, 2006
  4. Replies:
    7
    Views:
    491
    Jim Langston
    Jul 13, 2005
  5. Mike P

    Generating random strings

    Mike P, May 31, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    304
    Samuel R. Neff
    May 31, 2007
Loading...

Share This Page