Return to site

String Palindrome Program In Cobol

broken image


Palindrome is a string, which when read in both forward and backward way is same.

A string is a palindrome if reversing it we obtain the same string. Is palindrome x = x reverse x Or, applicative and point-free, with some pre-processing of data (shedding white space and upper case). The above problem can be recursively defined. Initial Values: i = 0, j = n-1; Given string 'str' CountPS(i, j) // If length of string is 2 then we // check both character are same or not If (j i+1) return stri strj Else If stri.j is PALINDROME // increment count by 1 and check for // rest palindromic substring (i, j-1), (i+1, j) // remove common palindrome substring (i+1, j-1.

Example:
radar, madam, pop, lol, etc.

Palindrome String Check Program in C

Explanation:

Fi 6140z driver. To check if a string is a palindrome or not, a string needs to be compared with the reverse of itself.

Consider a palindrome string: radar,

String handling statements in COBOL are used for performing multiple functional operations on strings. String handling statements String handling statements are: 1. Inspect verb counts or replaces the characters in a string. String operations are performed on alphanumeric, numeric, or alphabetic values. 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'.

---------------------------
index: 0 1 2 3 4

value: r a d a r
---------------------------

To compare it with the reverse of itself, the following logic is used:

String
  1. 0th character in the char array, string1 is same as 4th character in the same string.
  2. 1st character is same as 3rd character.
  3. 2nd character is same as 2nd character.
  4. . . . .
  5. ith character is same as 'length-i-1'th character.
  6. If any one of the above condition fails, flag is set to true(1), which implies that the string is not a palindrome.
  7. By default, the value of flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome.

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.

Cobol find string in string

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.

Example

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:

String Palindrome Program In Cobol Programming

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.

String Palindrome Program In Cobol
  1. 0th character in the char array, string1 is same as 4th character in the same string.
  2. 1st character is same as 3rd character.
  3. 2nd character is same as 2nd character.
  4. . . . .
  5. ith character is same as 'length-i-1'th character.
  6. If any one of the above condition fails, flag is set to true(1), which implies that the string is not a palindrome.
  7. By default, the value of flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome.

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:

String Palindrome Program In Cobol Programming

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:

  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:

Simple Java Palindrome Program

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

Cobol Programs Examples

COBOL Blog: Click Here IBM Reference:Click Here





broken image