Thursday, 1 September 2016

How to find special character in String using COBOL program

How to check special character in a string and replace with spaces using COBOL program.


01  TEMP-VAR                            PIC X(10) VALUE " INS#PECT ".
01  TEMP-VAR-LEN                   PIC 9(02) VALUE ZERO.
01  TEMP-VAR-COUNT             PIC 9(02) VALUE ZERO.
01  TEMP-CHAR-COUNT          PIC 9(02) VALUE ZERO.
01  TEMP-CHAR                         PIC X(10) VALUE SPACES.
01  TEMP-CHK-VAR                  PIC X(10) VALUE SPACES.


MOVE LENGTH OF TEMP-VAR TO TEMP-VAR-LEN.      

MOVE 0                   TO TEMP-VAR-COUNT.      
      
PERFORM VARYING TEMP-CHAR-COUNT FROM 1 BY 1                
            UNTIL TEMP-CHAR-COUNT > TEMP-VAR-LEN        

  MOVE  TEMP-VAR(TEMP-CHAR-COUNT:1) TO TEMP-CHAR  

  ADD 1                  TO TEMP-VAR-COUNT                

  EVALUATE TEMP-CHAR                                  
     WHEN ‘#’                                  
        MOVE SPACES TO TEMP-CHG-VAR(WS-VAR-COUNT:1)
     WHEN OTHER
        MOVE TEMP-CHAR TO TEMP-CHK-VAR(WS-VAR-COUNT:1)
  END-EVALUATE

END-PERFORM.


Here the logic is:
To get the length of the string and perform until the complete string has been checked, each word is checked using the EVALUATE and if the special character is found it will be replaced with spaces or the same character will be moved into the variable using reference modification. 

No comments:

Post a Comment

Expense Handler Application with advance technologies

Budget Planner  One of the application developed with Ionic 4 and Python-Flask.  Ionic 4 code:  https://github.com/logeshbuiltin/Expense...