Program to check whether the input string is palindrome or not: Sep 20: An Assembly Language Program to check for Palindrome string: Mar 24: Program to check entered string is palindrome or not: Dec 30: Program to check entered string is palindrome or not: Jan 22: Prolog program to check whether a given word is a palindrome or not: Feb 22. You can also use INSPECT to reverse a string. Code something like this to reverse an 8-character string SRC and get the result in TGT: MOVE x'030201' TO TGT INSPECT TGT CONVERTING x'060708' TO SRC Note that you can use this trick to reorder a field in any desired way. For strings that include multiple characters, this equality is true only when the sourcestring is a palindrome. For character strings where MOD(N,2) = 1, the character in ordinal position (N+1)/2 has the same middle position in both the sourcestring and in the returned string. It's very easy to check string is palindrome or not. Be sure efficiency is good or not.
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.
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:
- 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. - Because ALL SPACES is specified as a delimiter, the five contiguous SPACE characters are considered to be one occurrence of the delimiter.
- 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.
- 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.
- Positions 35 through 40 (475120) are examined and are placed in M-UNITS.
- 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. - 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.
- 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.
- 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.
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
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.
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
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
String Palindrome Program In Cobol Compiler
> stringletters :: String -> String
> stringletters str = filter isAlpha str
> stringLowerLetters :: String -> String
> stringLowerLetters = stringToLower.stringletters
> 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?
Cobol Programs Examples
If you want to learn more about functional programming, check one of the best books about it (Richard Bird – Thinking Functionally with Haskell)