Delete any word from a sentence



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Delete any word from a sentence

Postby common_guy » Wed Sep 16, 2015 5:16 pm

Input string (that is a sentence) is of max 80 length characters.

user will give the word which need to be deleted.
common_guy
 
Posts: 2
Joined: Wed Sep 16, 2015 5:08 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Delete any word from a sentence

 

Re: Delete any word from a sentence

Postby Robert Sample » Wed Sep 16, 2015 6:33 pm

Since you did not ask any questions nor indicate any issues, why did you post?

This is a pretty simple application of reference modification (or a table of characters). COBOL doesn't have any type of intrinsic search function but the logic is pretty straightforward.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 222 times

Re: Delete any word from a sentence

Postby common_guy » Sun Sep 20, 2015 10:27 am

Actually, by using inspect we can replace any word by another word of the same length. But while deleting suppose

My sentence is :

"My school is the best."

I want to delete the word "school".

My output will be :

"My is the best."

How to do that? Please give any suggestion.
common_guy
 
Posts: 2
Joined: Wed Sep 16, 2015 5:08 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Delete any word from a sentence

Postby Robert Sample » Sun Sep 20, 2015 5:08 pm

1. Count the characters in the word.
2. Find the word in your sentence.
3. Use reference modification (or an array) to move all characters up to your word to your output variable.
4. Use reference modification (or an array) to move all characters after your word to the correct location in your output variable.

If the word can occur more than once in the sentence, and each occurrence is to be deleted, then repeat steps 2 through 4 until you hit the end of the sentence (or at least until the word can no longer occur in the sentence). Key points: use an output variable instead of trying to do it in one variable; you'll need several variables for location and length; and you won't go wrong to run through a couple of examples with paper and pen to make sure you understand what the code is doing.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 222 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post