Checking character in String

295 pts.
Tags:
AS/400
COBOL
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?
ASKED: March 6, 2008  7:05 AM
UPDATED: March 6, 2008  1:19 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following