0% found this document useful (0 votes)
56 views37 pages

Introduction To String Handling EXAMINE Verb INSPECT Verb STRING Verb Unstring Verb

The document discusses string handling operations in COBOL such as examining, inspecting, concatenating, and segmenting strings using verbs like EXAMINE, INSPECT, STRING, and UNSTRING. It provides the syntax and examples of using these verbs to scan, replace, concatenate, and segment string data. Key operations include tallying, replacing, and moving strings between receiving and sending fields.

Uploaded by

Satyabrata Dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views37 pages

Introduction To String Handling EXAMINE Verb INSPECT Verb STRING Verb Unstring Verb

The document discusses string handling operations in COBOL such as examining, inspecting, concatenating, and segmenting strings using verbs like EXAMINE, INSPECT, STRING, and UNSTRING. It provides the syntax and examples of using these verbs to scan, replace, concatenate, and segment string data. Key operations include tallying, replacing, and moving strings between receiving and sending fields.

Uploaded by

Satyabrata Dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Chapter 7

 Introduction to String handling C


 EXAMINE verb
 INSPECT verb
 STRING verb R
 UNSTRING verb

S
d

D 1
Chapter 7

R
7.1 INTRODUCTION TO STRING HANDLING

S
d

D 2
Introduction to String handling

 Programs often require that data be moved from C


one area to another and / or that operations be
performed on the individual characters of a data
set.
 String handling operation includes
R
 Scanning and replacement – EXAMINE &

S
d INSPECT verb
 Concatenation – STRING verb
 Segmentation - UNSTRING verb

D 3
Chapter 7

7.2
R
EXAMINE VERB

S
d

D 4
EXAMINE - Purpose & Syntax for Format 1
Purpose
 C
To scan a string to find the number of occurrences
of a given character in it.
 It can also be used to replace some or all

R
occurrences of the said character by another
character
Syntax

S
dFormat 1:

ALL
EXAMINE {identifier-1} TALLYING LEADING

D
UNTIL FIRST
literal-1. 5
Example

C
WORKING-STORAGE SECTION.
01 EXAMINE-DATA.
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
05 E-2 PIC 9(10) VALUE 0150250350.
………..
PROCEDURE DIVISION. R
TEST-1.
EXAMINE E-1 TALLYING ALL ‘D’.
S
d
IF TALLY NOT EQUAL TO ZERO
PERFORM PROCESS-PARA
ELSE

D
PERFORM EXIT-PARA.
6
Example contd

01 EXAMINE-DATA.
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
C
05 E-2 PIC 9(10) VALUE 0150250350.

R
 EXAMINE E-2 TALLYING LEADING 0.

S
d
 EXAMINE EXAMINE-DATA TALLYING UNTIL
FIRST 2.

D 7
Format 2 - Syntax

 Format 2 C
ALL
EXAMINE {identifier} REPLACING LEADING
R FIRST
UNTIL FIRST
Literal-1 BY literal-2
S
d

D 8
Format 2 - Example
01 EXAMINE-DATA.
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
C
05 E-2 PIC 9(10) VALUE 0150250350.

R
 EXAMINE E-1 REPLACING ALL “A” BY “E”.

S
d
 EXAMINE E-1 REPLACING LEADING “A” BY “E”.

D 9
Format 2 - Example

01 EXAMINE-DATA. C
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
05 E-2 PIC 9(10) VALUE 0150250350.
R
 EXAMINE E-1 REPLACING FIRST “D” BY “L”.

S
d

 EXAMINE E-1 REPLACING UNTIL FIRST “D” BY “L”.

D 10
Format 3 – Syntax & Example

C
 Format 3
ALL
EXAMINE {identifier} TALLYING LEADING

R UNTIL FIRST
REPLACING BY literal-2
 Example
S
d
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.

EXAMINE E-1 TALLYING ALL “D” REPLACING


D
BY “M”.
11
Chapter 7

7.3
R
INSPECT VERB

S
d

D 12
Points on INSPECT verb

 Replacement for EXAMINE verb C


 Provides facility to store the count in a user-defined
data name
R
 Permits several tallies and replacements with one
statement
 Allows group of characters to be counted and

S
d
replaced
 Used only with alphanumeric data item

D 13
Format 1 – Syntax

INSPECT identifier-1 TALLYING identifier-2 C


ALL identifier-3
FOR LEADING Rliteral-1
CHARACTERS

S
d
BEFORE identifier-4
INITIAL
AFTER literal-2
D 14
Example

77 ID-1 PIC X(15) VALUE ‘AFFFBCGHIFFFJKL’.


77 ID-2 PIC 99 VALUE 0.
C
77 ID-3 PIC 99 VALUE 0.
77 ID-4 PIC 99 VALUE 0
R
 INSPECT ID-1 TALLYING ID-2 FOR ALL “F”.

S
d
 INSPECT ID-1 TALLYING ID-3 LEADING “F”.

 INSPECT ID-1 TALLYING ID-4 FOR ALL “F” BEFORE

D
INITIAL “G’.
15
Example contd

77 ID-1 PIC X(15) VALUE ‘AFFFBCGHIFFFJKL’. C


77 ID-5 PIC 99 VALUE 0.
77 ID-6 PIC 99 VALUE 0.
R
 INSPECT ID-1 TALLYING ID-5 FOR
CHARACTERS BEFORE INITIAL “I”.

S
d
 INSPECT ID-1 TALLYING ID-6 FOR
CHARACTERS AFTER INITIAL “I”.

D 16
Format 2 - Syntax
INSPECT identifier-1 REPLACING
C
CHARACTERS BY {id-2/lit-1}

BEFORE
AFTER
INITIAL {id-6/lit-5}
R
ALL

S
d
LEADING {id-4/lit-3} BY {id-5/lit-4}
FIRST
BEFORE INITIAL {id-6/lit-5}

D
AFTER

17
Example

77 ID-1 PIC X(15) VALUE ‘AFFFBCGHIFFFJKL’. C


 INSPECT ID-1 REPLACING CHARACTERS BY
“*” AFTER INITIAL “I”.
R
 INSPECT ID-1 REPLACING CHARACTERS BY
“$” BEFORE INITIAL “B”.

S
d

 INSPECT ID-1 REPLACING ALL “F” BY “&”.

D 18
Example contd

C
77 ID-1 PIC X(15) VALUE ‘AFFFBCGHIFFFJKL’.
 INSPECT ID-1 REPLACING FIRST “FFF” BY “$$
$”.
R
 INSPECT ID-1 REPLACING LEADING “A” BY “&”.

S
d

D 19
Format 3 – Syntax & Example

 Syntax C
INSPECT identifier-1 TALLYING
<tallying part as in format 1>
REPLACING R
<replacing part as in format 2>.
 Example

S
d
INSPECT ID-1
TALLYING ID-2 FOR CHARACTERS BEFORE INITIAL “I”
REPLACING CHARACTERS BY “*” AFTER INITIAL “I”.

D 20
Chapter 7

7.4
R
STRING VERB

S
d

D 21
Purpose of STRING verb

 Two or more strings of characters can be C


combined

R
 To transfer characters from a string to another
string starting at some particular character position
either in the receiving field or in the sending field

S
d

D 22
Syntax

STRING {id-1 / lit-1} , {id-2 / lit-2},….


DELIMITED BY {id-3 / lit-3/SIZE}
C

R
{id-4 / lit-4} , {id-5 / lit-5},….
DELIMITED BY {id-6 / lit-6/SIZE}

INTO {id-7} [ WITH POINTER id-8 ]

S
d
[ ON OVERFLOW imperative-statement ]

D 23
Example 1

01 DayStr PIC XX. 5 C


01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
R
01 DateStr PIC X(15) VALUE ALL "-".
- - - - - - - - - - - - - - -

S
STRING
d DayStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
", " DELIMITED BY SIZE

D YearStr DELIMITED BY SIZE


INTO DateStr 24
Example 2

01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
C
02 Field2 PIC X(30)

R
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.

S
02
d StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Field1 DELIMITED BY SPACES INTO Field2.
DISPLAY Field2.
D 25
Example 3

01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this
C
go".
02 Field2 PIC X(30)

R
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.

S
d02 StrPtr PIC 99.
02 NewPtr PIC 9.

STRING Field1 DELIMITED BY SIZE INTO Field2.

D
DISPLAY Field2.

26
Example 4

01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
C
02 Field2 PIC X(30)
VALUE "This is the destination string".

R
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.

S
02
d NewPtr PIC 9.
MOVE 6 TO StrPtr.
STRING Field1, Field3 DELIMITED BY SPACE
INTO Field2 WITH POINTER StrPtr

D
ON OVERFLOW DISPLAY "String Error"
NOT ON OVERFLOW DISPLAY Field2.
27
Example 5
01 StringFields.
C
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".

R
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
S
d
STRING Field1, Field2, Field3 DELIMITED BY SPACES
INTO Field4.
DISPLAY Field4

D 28
Chapter 7

R
7.5 UNSTRING VERB

S
d

D 29
Purpose of UNSTRING verb

 Used to split one string to many substrings. C


 As like STRING verb, based on the delimiter
specified, the splitting occurs R

S
d

D 30
Syntax

UNSTRING id-1 C
DELIMITED BY [ALL] {id-2/lit-1}, OR [ALL] {id-3/lit-2}

INTO id-4 R
[ , DELIMITER IN id-5 ]
[ , COUNT IN id-6 ]
[ id-7 [ , DELIMITER IN id-8 ]
S
d
[ , COUNT IN id-9 ]
[ WITH POINTER id-10 ]
[ TALLYING IN id-11 ] [; ON OVERFLOW imperative-stmt]

D 31
Example 1

01 DayStr PIC XX.


C
01 MonthStr PIC XX.

R
01 YearStr PIC XX.
01 DateStr PIC X(8).

1 9 -0 5 - 8 0

S
dACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left“.

D 32
Example 2

01 DayStr PIC XX.


C
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(8).
R
1 9 s t o p 0 5 s t o p 8 0

S
d

ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
D ON OVERFLOW DISPLAY "Chars Left“.
33
Example 3
01 DayStr PIC XX.
01 MonthStr PIC XX.
C
01 YearStr PIC XX.

R
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0

ACCEPT DateStr.
UNSTRING DateStr

S
dDELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr.

D
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2 34
Example 4

01 DayStr PIC XX. C


01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(11). R
1 5 - - - 0 7 - - 9 4

S
d
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY ALL "-"

D
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left“.
35
Chapter 7

7.6
R
REFERENCE MODIFICATION

S
d

D 36
Reference Modification – Syntax & Example

C
 To refer a part of a data field by specifying the left
most character position and length.

 Syntax:

R
Data name (left-most character position: length)

 Example:

S
d 01 SAMPLE-DATA.
05 S1 PIC X(8) VALUE “SOFTWARE”.
05 S2 PIC X(3).
MOVE S1(4: 3) TO S2.
D 37

You might also like