Tuesday, May 1, 2012

Remove Multiple spaces to one in COBOL

  
 Hi Guys
 I was asked this question in some interview and find it interesting .Please check the below given program and do suggest me if you have some better solution

      identification division.
       program-id.   bdemo.

       environment division.
       input-output section.
       file-control.
           select infile assign to "../xyz/input.txt"
            organization is line sequential.
           select outfile assign to "../xyz/output.txt"
            organization is line sequential.
           select workfile assign to "../xyz/temp.txt".
                     
       data division.
       file section.
      
       fd  infile
           label records are standard
           record contains 132 characters
           data record is in-rec.

       01  in-rec.
           05  filler                       pic x(132).
       
       fd  outfile
           label records are standard
           record contains 132 characters
           data record is out-rec.
       01  out-rec.
           05 filler                        pic x(132).     
      
       working-storage section.
      
       01  ws-end-of  pic x.
           88 ws-eof   value 'Y'.
           88 ws-not-eof   value 'N'.
       01 ws-space pic x.
           88 first-char   value 'Y'.
           88 first-space  value 'N'.
          
       01  i pic 9(3) value 1.
       01  j pic 9(3) value 1.
       01  ws-left pic 9(3) value zeros.
          
                     
       linkage section.    
      
     
       procedure division .

       1000-pgm-start.
     
       display "ksingh".
      
       open input infile .
       open output outfile.
       set ws-not-eof to true.
      
      
       perform display-para until ws-EOF.
      
       close infile.
       close outfile.         
       exit program.
      
       remove-spaces.
         move 1 to i.
         move 1 to j.
         set first-space to true.
         if in-rec(i:1) = space           
            set first-space to true
         else   
            set first-char to true
         end-if.
           
         move 1 to i.
         move 1 to j.           
        
         perform 132 times
          if in-rec(i:1) = space           
            add 1 to i
            set first-space to true    
      
           else                   
              if (first-space)
                 move " " to out-rec(j:1)
                 add 1 to j
              end-if
            set first-char to true
            move in-rec(i:1) to out-rec(j:1)
            add 1 to i
            add 1 to j        
           end-if           
         end-perform.
         compute ws-left = 132 - j .
         move spaces to out-rec(j:ws-left).
         write out-rec.      
      
       remove-spaces-end.
      
       display-para.
      
       read infile at end set ws-EOF to true.
      
       if (not ws-eof)
      
       perform remove-spaces  thru
                  remove-spaces-end
            
      
       display in-rec.
      
       display-para-exit.
       exit



cob_ques

0 comments:

Post a Comment