string handling by raj

Upload: prasad0803

Post on 05-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 String Handling by Raj

    1/18

    String HandlingHandling strings in COBOL

    [In this document I have given comprehensive details tohandle the Strings in COBOL. I have explained how to useSTRING, UNSTRING, INSPECT, Reference modification andbuilt-in string functions with all options. To make you doubtfree, I have given examples and the expected output for allthe examples with explanation text. I hope it will reachyour expectations. If you have any queries or to add moreto this document, you can reach me [email protected]]

    Rajendra Prasad [email protected]

  • 8/2/2019 String Handling by Raj

    2/18

    String Handling

    In COBOL string handling can be done by using the following

    STRING

    UNSTRING

    INSPECT

    Reference Modification

    Intrinsic or Built-in functions.

    STRING statement

    The STRING verb can be used to concatenate the two or more strings.As usual with alphanumeric to alphanumeric MOVEs, data movement is from left to right.The leftmost character of the source is moved to the leftmost position of the destination then the neleftmost of the source to the next leftmost of the destination and so on.

    Delim$il : it is a character or set of characters in a source string that terminates the data transfer tdestination string.Pointer#i :point or location in the destination string from where the next character has to copy. The pointmust be a positive integer.DELIMITED BY SIZE: it says that irrespective of delimiter in source string, the entire string will be copieinto destination string.ON OVERFLOW: this means, if there are still valid characters strings to copy into destination string, bdestination string is full then ON OVERFLOW statements will be executed.

    The data movement from source strings to destination string is terminated if

    The end of any source string is reached. The end of destination string is reached.

    The delimiter is detected.

    The destination string must be an elementary data-item with no editing symbols or JUSTIFIED RIGHclause.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    3/18

    E.g.

    Output

    E.g. for ON OVERFLOW phrase

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    4/18

    Output

    E.g. for WITH POINTER phrase

    Output

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    5/18

    UNSTRING statement

    The UNSTRING can be used to divide a string into sub strings based on specified delimiter.

    DELIMITED BY Delim$il indicates that a character or set of characters in the source string that stops dattransfer to destination string.DELIMITER IN HoldDelim$i it contains the delimiter that caused stopped data transfer to particuldestination string.COUNT IN CharCounter#i it contains value of no. of characters(count) that are copied to particuldestination string.WITH POINTER Pointer#i it contains position in the source string from which the next character will b

    taken.TALLYING IN DestCounter#i it contains the value that no. of destination strings affected by UNSTRINoperation.ON OVERFLOW statements these statements will be executed if

    The data is still there in source string after all sub strings are filled up.

    If the pointer that contains value less than one or it has the value which is higher than length osource string.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    6/18

    E.g.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    7/18

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    8/18

    Output

    The INSPECT statement

    The INSPECT can be used to count, replace and convert the characters in a string.The INSPECT has four formats.

    The first format is used to counting characters in a string.

    The second format replaces group of characters in a string with another group of characters.

    The third format combines both in one statement.

    The fourth format converts each set of characters to its corresponding character in another set ocharacters.

    The counting INSPECT

    TALLYING Counter#i it contains the total value of occurrences count of the literal or character or group ocharacters in source sting.

    FOR ALL CHARACTERS this indicates that the TALLYING variable should have the total no. characters in source string.FOR LEADING this indicates that the TALLYING variable should have value that is count of leading zeroor spaces or characters.AFTER INITIAL Delim$il this indicates that TALLYING variable should have the value that is count ocharacters that are there after initial occurrence specified character or literal (delimiter).BEFORE INITIAL Delim$il this indicates that TALLYING variable should have the value that is count characters that are there before specified character or literal (delimiter).

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    9/18

    E.g. To find the no. of occurrences of specified literal.

    Output

    E.g. for AFTER INTITAL & BEFORE INITIAL and LEADING

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    10/18

    OUTPUT

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    11/18

    The Replacing INSPECT

    This format of INSPECT can be used to replace one or more characters with another one or more characterRules

    When replacing, the size of given characters and the size of replacing characters must be same.

    When there is CHARACTERS phrase, each character matched in the source is replaced by singgiven replacing character.

    When FIRST is there, then the left most occurrence will be replaced with the given character.

    E.g. TO REPLACE LEADING SPACES WITH *S

    Output

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    12/18

    e.g. Replacing all the characters in a string with given character.

    Output

    e.g for AFTER INTIAL BEFORE INITIAL

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    13/18

    Output

    The combined INSPECT

    This format of INSPECT allows you to do counting and replacing in one statement.

    e.g:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    14/18

    Output

    The converting INSPECT

    This format of the INSPECT is used to convert one set of characters with another set of characters.Rules

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    15/18

    The length of both set of characters (actual and converting) must be same.

    The first set of characters (the converting characters) should not have any duplicate characters.

    In CONVERTING, the set of characters to be converted and replacing set of characters will be donorderly.

    e.g INSPECT pandari CONVERTING pr to *#. = *anda#iThen p replaced with * and r replaced with #.

    e.g.

    Output

    Reference Modification

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    16/18

    Reference Modification allows you to treat a numeric or alphanumeric data-items as if it are an array ocharacters. To access the substring using reference modification you must specify

    The name of data-item.

    The start character position of sub-string.

    The number of characters in sub-string.The syntax is

    DATA-ITEM (start-position [: length])

    E.g.

    Output

    E.g. To remove leading spaces from string using INSPECT TALLYING & Ref. modification.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    17/18

    Output

    Built-in functions

    FUNCTION LENGTH(string)

    FUNCTION REVERSE(string)

    FUNCTION UPPER-CASE(string)

    FUNCTION LOWER-CASE(string)

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity, while creativity is mastery of simplicity

  • 8/2/2019 String Handling by Raj

    18/18

    E.g.

    Output

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Technical skill is mastery of complexity while creativity is mastery of simplicity