identification division.
program-id. parse-sentence.
data division.
working-storage section.
01 long-sentence pic x(500) value
"When in the Course of human events, it becomes necessary
- "for one people to dissolve the political bands which have
- "connected them with another, and to assume among the powers
- "of the earth, the separate and equal station to which the
- "Laws of Nature and of Nature's God entitle them, a decent
- "respect to the opinions of mankind requires that they
- "should declare the causes which impel them to the
- "separation.".
01 shorter pic x(40).
01 shorter-length pic 999.
01 a-word pic x(30).
01 a-word-length pic 99.
01 word-count pic 999 value 0.
01 in-ptr pic 999.
01 out-ptr pic 999.
88 is-max-out-ptr value 999.
01 pic x.
88 is-first value "F" false "L".
01 the-delimiter pic x.
88 is-full-stop value "." false space.
procedure division.
a.
set is-full-stop to false.
set is-first to true.
move 1 to in-ptr.
set is-max-out-ptr to true.
move all "A" to a-word.
move 0 to shorter-length.
inspect shorter
tallying shorter-length for characters.
perform until is-full-stop
move spaces to a-word
unstring long-sentence
delimited by all spaces or "." or ","
into a-word
delimiter in the-delimiter
count in a-word-length
with pointer in-ptr
tallying in word-count
end-unstring
if a-word-length > 0 then
if (out-ptr + a-word-length) > shorter-length
if is-first
set is-first to false
else
display shorter
end-if
move 1 to out-ptr
move spaces to shorter
end-if
string a-word (1:a-word-length), " "
delimited by size
into shorter
with pointer out-ptr
end-string
else
subtract 1 from word-count
end-if
end-perform.
if out-ptr > 1 display shorter.
display "Word count = ", word-count.
stop run.