Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

In the string, check the matching of the opening ((and closing)) brackets.

0 Kudos

Display the number of pairs of brackets, if matching, or the message “ERROR” otherwise. In the string with the form ") (" no match is performed.

1)Input data

((Hello)) (Bob) (!)

output

4

2)input

)( Hi!

output

ERROR

1 ACCEPTED SOLUTION

marcobeer
Active Participant

Hi Symbat,

Just the same way like in your other question.
https://answers.sap.com/questions/13347248/count-the-number-of-times-the-substring-occurs-in.html?ch...

Compare the both values of FIND ALL OCCURRENCES OF.

FIND ALL OCCURRENCES OF '(' IN lc_string IGNORING CASE MATCH COUNT DATA(li_open).
FIND ALL OCCURRENCES OF ')' IN lc_string IGNORING CASE MATCH COUNT DATA(li_close).
IF li_open <> li_close.
  MESSAGE 'Number of brackets do not match.' TYPE 'E'.
ENDIF.

Best Regards
Marco

5 REPLIES 5

marcobeer
Active Participant

Hi Symbat,

Just the same way like in your other question.
https://answers.sap.com/questions/13347248/count-the-number-of-times-the-substring-occurs-in.html?ch...

Compare the both values of FIND ALL OCCURRENCES OF.

FIND ALL OCCURRENCES OF '(' IN lc_string IGNORING CASE MATCH COUNT DATA(li_open).
FIND ALL OCCURRENCES OF ')' IN lc_string IGNORING CASE MATCH COUNT DATA(li_close).
IF li_open <> li_close.
  MESSAGE 'Number of brackets do not match.' TYPE 'E'.
ENDIF.

Best Regards
Marco

0 Kudos

hello , I wanted to ask if there is anything else to add to the code , since the program gives an error

marcobeer
Active Participant
0 Kudos

Hello Symbat,

Well, yes. You need to define "lc_string" before using it. Right after your REPORT statement please put:

DATA lc_string TYPE C LENGTH 64 VALUE 'Hello ) ( World )'.

Additionally, your Report-Name and the name stated at your REPORT statement do not match, they should be the same (even if it works if they are not).

REPORT ZY_HELLO_88.

EDIT: Based on the error, it seems to me that you are using a very old SAP Version. Which release are you working on?

Best Regards
Marco

0 Kudos

version 2005

marcobeer
Active Participant
0 Kudos

That's a very old version, you shouldn't start learning on such an old version. Many functions will be missing there and you'll always stumble over issues, which don't happen in newer versions.

For this specific task, you could replace the FIND statement with something like:

li_length = strlen( lc_string ).
DO li_length TIMES.
  IF lc_string+li_index(1) = '('.
    ADD 1 TO li_open.
  ENDIF.
  ADD 1 TO li_index.
ENDDO.

Same for the closing brackets for sure.

Best Regards
Marco