-
December 30th, 2020, 11:14 AM
#1
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
-
December 30th, 2020, 11:38 AM
#2
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
-
December 31st, 2020, 04:33 AM
#3
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 31st, 2020, 12:02 PM
#4
Re: string replace related
Originally Posted by pdk5
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
-
December 31st, 2020, 12:28 PM
#5
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 1st, 2021, 05:05 AM
#6
Re: string replace related
@wolle and kaud: Thanks a lot
-
January 1st, 2021, 06:13 AM
#7
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++23 Compiler: Microsoft VS2022 (17.6.5)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|