String Palindrome Program In Cobol



Palindrome Partitioning Dynamic Programming Data Structure Algorithms In this algorithm, the input is a string, a partitioning of that string is palindrome partitioning when every substring of the partition is a palindrome. In this algorithm, we have to find the minimum cuts are needed to palindrome partitioning the given string. What is a Palindrome? # A number or a word which remains the same, even after being reversed is called palindrome. For example, mom, radar, or the number 45654, all are palindrome. The following is a C program to determine whether a string is a palindrome or not. Exercise - String 1. Write a program to check if a given string is a Palindrome. A palindrome reads same from front and back e.g.- aba, ccaacc, mom, etc. Write a program to find out the largest and smallest word in the string 'This is an umbrella'. Jul 11, 2018 Dynamic Programming Data Structure Algorithms In this algorithm, the input is a string, a partitioning of that string is palindrome partitioning when every substring of the partition is a palindrome. In this algorithm, we have to find the minimum cuts are needed to palindrome partitioning the given string. If the string is a one-character Palindrome, then it isn’t possible to do so since every one-character string is a Palindrome. Then, we can scan the first half of the Palindrome to see if it is all ‘a’. If it is, then we change the last character to ‘b’. Otherwise, we change the first non ‘a’ character to ‘a’.

The UNSTRING statement used to separate single string to multiple based on the delimiter provided in the UNSTRING. It needs at least two destination identifier or literals. It can only separate alphabetic and alpha-numeric items. END-STRING, DELIMITED BY, TALLYING, WITH POINTER, ON OVERFLOW and NOT ON OVERFLOW clause is optional in UNSTRING usage.

UNSTRING statement Syntax

COUNT IN: COUNT IN clause associated with a particular destination data item. COUNT IN holds the count of no of character passed to the particular destination identifier/data item.

TALLYING:TALLYING clause holds the count of destination strings affected by UNSTRING. TALLYING clause associated with all the destination data items.

UNSTRING Statement Example

In the Data Division, the user has defined the following input record to be acted upon by the UNSTRING statement:

The next two records are defined as receiving fields for the UNSTRING statement. DISPLAY-REC is to be used for printed output. WORK-REC is to be used for further internal processing.

The user has also defined the following fields for use as control fields in the UNSTRING statement.

Cobol String Command Example

In the Procedure Division, the user writes the following UNSTRING statement to move subfields of INV-RCD to the subfields of DISPLAY-REC and WORK-REC:

Before the UNSTRING statement is issued, the user places the value 3 in the CHAR-CT (the pointer item), so as not to work with the two control characters at the beginning of INV-RCD. In DBY-1, a period is placed for use as a delimiter, and in FLDS-FILLED (the tallying item) the value 0 is placed. The following data is then read into INV-RCD

When the UNSTRING statement is executed, the following actions take place:

  1. Positions 3 through 18 (FOUR-PENNY-NAILS) of INV-RCD are placed in
    ITEM-NAME, left-justified within the area, and the unused character positions are padded with spaces. The value 16 is placed in CTR-1.
  2. Because ALL SPACES is specified as a delimiter, the five contiguous SPACE characters are considered to be one occurrence of the delimiter.
  3. Positions 24 through 29 (707890) are placed in INV-NO. The delimiter character / is placed in DLTR-1, and the value 6 is placed in CTR-2.
  4. Positions 31 through 33 are placed in INV-CLASS. The delimiter is a SPACE, but because no field has been defined as a receiving area for delimiters, SPACE is merely bypassed.
  5. Positions 35 through 40 (475120) are examined and are placed in M-UNITS.
  6. The delimiter is a SPACE, but because no receiving field has been defined as a receiving area for delimiters, SPACE is bypassed. The value 6 is placed
    in CTR-3.
  7. Positions 42 through 46 (00122) are placed in FIELD-A and right-justified within the area. The leftmost character position is filled with a 0 (zero). The delimiter is a SPACE, but because no field has been defined as a receiving area for delimiters, SPACE is bypassed.
  8. Positions 48 through 53 (000379) are placed in DISPLAY-DOLS. The period delimiter character is placed in DLTR-2, and the value 6 is placed in CTR-4.
  9. Because all receiving fields have been acted upon and two characters of data in INV-RCD have not been examined, the ON OVERFLOW exit is taken, and execution of the UNSTRING statement is completed.

At the end of execution of the UNSTRING statement, DISPLAY-REC contains the following data:

CHAR-CT (the pointer field) contains the value 55, and FLD-FILLED (the tallying field) contains the value 6.

COBOL Blog: Click Here IBM Reference:Click Here

I could write this post by showing directly my solution and why it is efficient, but this is a kind of exposition that implies arrogance. Instead, I want to show you how functional reasoning allows you to write efficient programs.

To jump directly to the problem, we’ll start with a definition: a palindrome is a string read the same from start to end and from end to start, ignoring blank characters, punctuation symbols, and whether or not a letter is in lowercase or uppercase.

For example
– this is a palindrome: “Madam, I’m Adam”
– this is not a palindrome: “This man is working hard”

So the definition of a palindrome is easy to understand, and to test that a string is a palindrome or not is easy from the standpoint of a reader.

Let’s solve it using functional programming instead of imperative programming. The first thing to notice is that we have to test if a character is a letter or not. In Haskell, in the library Data.Char exists a function isAlpha :: Char -> Bool which tests if a character is a letter or not.
Also, in Haskell we have a function (imported from Data.Char) which coverts a letter in lowercase: toLower :: Char -> Char.

Palindrome program in python

The first thing to do with a string (let’s note it str) is to convert it in lower letters. We know that a string is a list of characters, so we’ll map toLower on each character of the string str:

stringToLower :: String -> String
stringToLower str = map toLower str

Than, we have to remove from consideration every character in the string that isn’t a letter; that means we have to filter the characters from the string which are not letters. We can do that using the function filter from Haskell:

stringletters :: String -> String
stringletters str = filter isAlpha str

Cobol

Next, we combine the 2 functions defined untill now into one, in order to obtain the string str in lowercase and consisting only of its letters:

stringLowerLetters :: String -> String
stringLowerLetters = stringToLower.stringletters

Another function that exists in Haskell is reverse :: [a] -> [a], defined generally, which takes a list and returns the same list, but in reverse order!

Now we’re ready to construct the function which states if a number is palindrome or not:

isPalindrome :: Srting -> Bool
isPalindrome str = stringLowerLetters str stringLowerLetters(reverse str)

Here comes an importat point to make! Notice that in the above definition of isPalindrome, we used 2 times the function stringLowerLetters. But in functional programming, when we construct functions from another functions, we can reason about functional equations. This is very importat, because it allows one to view clearly the problems of efficiency in terms of functional composition.

Cobol String Replace

In our definition, let’s see that stringLowerLetters . reverse = reverse . stringLowerLetters , which is the same with saying that converting string to lower letters, than reversing it, is the same with reversing the string, than converting it to lower letters. That permits for the following definition of isPalindrome, which uses stringLowerLetters only once! This is a huge improvement when you’re working with strings of large lengths.

The more efficient definition is the following::

isPalindrome :: String -> Bool
isPalindrome str = s1 reverse(s1)
where s1 = stringLowerLetters str

String Palindrome Program In Cobol Program

Here is an importand observation to make: composing programs from small functions allows one to reason about larger programs, and the properties of the small function components permits for simplifications and optimizations of the larger ones. We can say in a more obvious way, in the isPalindrome function, that simpler means more efficient.

Back to our problem, this is the final program in Haskell:

> import Data.Char
> import Data.List

> stringToLower :: String -> String
> stringToLower str = map toLower str

> stringletters :: String -> String
> stringletters str = filter isAlpha str

> stringLowerLetters :: String -> String
> stringLowerLetters = stringToLower.stringletters

Cobol Programs Examples

> isPalindrome :: String -> Bool
> isPalindrome str = s1 reverse(s1)
> where s1 = stringLowerLetters str

As an exercise, you can write an efficient algorithm for isPalindrome using recursion. How would you do it?

String Palindrome Program In Cobol Compiler

If you want to learn more about functional programming, check one of the best books about it (Richard Bird – Thinking Functionally with Haskell)