285 pts.
 Checking character in String
In COBOL how to check a particular character in a string variable? for example: 01 WS-STRING PIC X(10) VALUE 'GOOD DAYYY' now i want to search 'D' in variable WS-STRING. So how should I do it? Do we have any built in function as in RPGLE we have SCAN and CHECK?

Software/Hardware used:
ASKED: March 6, 2008  7:05 AM
UPDATED: March 6, 2008  1:19 PM

Answer Wiki:
I'm not sure I really understand your question. The text asks to check for a particular character ('D') but then you reference the CHECK built-in which checks for the absence of a character and SCAN which locates the first occurence of the character. Assuming that what you want is "check a particular character in a string variable" then: <pre> DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-String PIC X(10) VALUE "GOOD DAYYY". 01 Number-Found PIC S9(9) BINARY VALUE 0. PROCEDURE DIVISION. MAIN-LINE. INSPECT WS-String TALLYING Number-Found FOR ALL "D". DISPLAY Number-Found. STOP RUN. </pre> should do the trick. After the INSPECT if Number-Found = 0 then no "D" was found. If Number-Found is > 0 then that is the number of "D" occurences within WS-String. Note that Number-Found needs to be re-initialized prior to each INSPECT. The documentation for INSPECT can be found in the <a href="http://publib.boulder.ibm.com/infocenter/iseries/v5r4/topic/books/sc092539.pdf">ILE COBOL Language Reference</a> in the i5/OS Information Center. Bruce Vining <a href="http://www.brucevining.com/">http://www.brucevining.com/</a> Integrated solutions for the System i user community
Last Wiki Answer Submitted:  March 6, 2008  11:13 am  by  bvining   6,055 pts.
All Answer Wiki Contributors:  bvining   6,055 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _