CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: string replace related

  1. #1
    Join Date
    May 2015
    Posts
    450

    string replace related

    Hello,

    Im trying to solve the following:
    Basically for the given input string, i need to expand as follows:

    4[x] = xxxx
    2[y2[z]] = yzzyzz
    2[pq] = pqpq

    thanks
    pdk

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,042

    Re: string replace related

    The possible way would be implementing a recursive function that searches for the expression of the form
    Code:
    <some_digit>[some_substring]

    and parse it.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,506

    Re: string replace related

    Presumably then this is valid?

    2[y2[3[zq2[p]]wx3[4[b]]c]]3[d]

    The issue is the possible nested brackets. I'd suggest first doing a proper syntax diagram and think about parsing it as you would an expression - possibly using
    a 'simple' recursive descent parser. You might also want to think about using a 'shunting yard' algorithm.
    Last edited by 2kaud; December 31st, 2020 at 12:32 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.10.4)

  4. #4
    Join Date
    Feb 2017
    Posts
    608

    Re: string replace related

    Quote Originally Posted by pdk5 View Post
    Im trying to solve the following:
    And you are not the only one . I recognize the problem because I replied to this thread,

    https://forums.codeguru.com/showthre...pressed-string

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,506

    Re: string replace related

    Thanks, wolle. I remember now. It did seem vaguely familiar......

    Given an input of (from post #3):

    2[y2[3[zq2[p]]wx3[4[b]]c]]3[d]

    My code from the specified thread post #9 gives an output of:

    yzqppzqppzqppwxbbbbbbbbbbbbczqppzqppzqppwxbbbbbbbbbbbbcyzqppzqppzqppwxbbbbbbbbbbbbczqppzqppzqppwxbbbbbbbbbbbbcddd

    which seems OK (?) [PS wolle's code gives the same output]

    For the given examples in post #1,

    Code:
    4[x]
    xxxx
    
    2[y2[z]]
    yzzyzz
    
    2[pq]
    pqpq
    Last edited by 2kaud; December 31st, 2020 at 12:34 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.10.4)

  6. #6
    Join Date
    May 2015
    Posts
    450

    Re: string replace related

    @wolle and kaud: Thanks a lot

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,506

    Re: string replace related

    Note that code assumes a correctly formatted input string. It's 'undefined behaviour' if it's not! As an exercise, you might want to study both and add format error detection and recovery. What about a number within []. ie 2[3] ???

    Also, to generate the repeated string, code such as str += ch is used within a loop. However, you could overload operator* for string with a number parameter and a string to return the repeated string. I'll leave it as an exercise....
    Last edited by 2kaud; January 1st, 2021 at 06:24 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.10.4)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)