From: grimlok@hubcap.clemson.edu (Mike Percy)
Subject: Re: new questions - getting rid of spaces
Organization: Clemson University
Date: Fri, 2 Oct 1992 15:05:24 GMT

cs3ea3cd@maccs.dcss.mcmaster.ca (Abrams       J) writes:

>Thanks to everyone who answered my original question about converting
>lower case to upper case in cobol.  You help was much appreciated!

>I have another questions now.  Does anyone know a method to remove spaces
>from a string in watcom cobol?  I cannot use the inspect/replacing 
>method because you must replace each string with a string of the same
>size, i.e. a space cannot be replaced with a null character.
 
Check out UNSTRING ... INTO ... DELIMITED BY ALL SPACES, which can be
used to extract "words" from a data-item, and then if you want, you can
use STRING ... INTO ... to put them back together again, adding exactly
as many spaces (or none) as you need.

This sounds like a homework problem, but...

working-storage section.
01  test-str  pic x(80) value "some words  are in here     and   some spaces". 
01  word-table.
    04  one-word pic x(40) occurs 10 times.
01  i pic 9(3).

procedure division.
0000-mainline section.
0000-para.
    unstring test-str delimited by all spaces
           into one-word (1)
                one-word (2)
                one-word (3)
                one-word (4)
                one-word (5)
                one-word (6)
                one-word (7)
                one-word (8)
                one-word (9)
                one-word (10)
           on overflow display "too many words".  
    string one-word (1) delimited by spaces          
           one-word (2) delimited by spaces          
           one-word (3) delimited by spaces          
           one-word (4) delimited by spaces          
           one-word (5) delimited by spaces          
           one-word (6) delimited by spaces          
           one-word (7) delimited by spaces          
           one-word (8) delimited by spaces          
           one-word (9) delimited by spaces          
           one-word (10) delimited by spaces         
           into test-str.                            
    perform varying i from 1 by 1 until i > 10       
        display "one-word (" i ") = " one-word(i)    
    end-perform.                                     
    display "after unstring/string: " test-str.           

running this gives:
one-word (001) = some                                 
one-word (002) = words                                
one-word (003) = are                                  
one-word (004) = in                                   
one-word (005) = here                                 
one-word (006) = and                                  
one-word (007) = some                                 
one-word (008) = spaces                               
one-word (009) =                                      
one-word (010) =                                      
after unstring/string: somewordsareinhereandsomespaces
 
Of course, this is cumbersome, and could result in some strange things
on overflow or too-long words.

Another way:

01 i pic 9(3).
01 j pic 9(3).
01 from-str pic x(80).
01 to-str pic x(80).

move 1 to j.
perform varying i from 1 by 1 until i > 80
    if from-str(i:1) not = spaces
        move from-str(i:1) to to-str(j:1)
        add 1 to j
    end-if
end-perform.

Mike Percy             | grimlok@hubcap.clemson.edu  | I don't know about
Sr. Systems Analyst    | mspercy@clemson.clemson.edu | your brain, but mine
Info. Sys. Development | mspercy@clemson.BITNET      | is really...bossy.
Clemson University     | (803) 656-3780              | (Laurie Anderson)
Xref: sparky alt.sewing:2887 rec.crafts.textiles:1741