Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Palindrome

Palindrome is a string that reads the same from both sides. The string s is given. Find the largest length of the substring that is not a palindrome.

Input data

The input file contains the string s. It consists only of lowercase letters of the Latin alphabet, is not empty, its length does not exceed 100 characters.

Output

In the output file, output the answer to the task, if there are several answers, select the lexicographically minimal one. If all substrings of s are palindromes, output NO SOLUTION to the output file.

example:

1)Input data

abba

output

abb

2) input data

aaaaaaaaa

ouput

no solution

3)input

abcghgcba

output

abcghgcb

1 ACCEPTED SOLUTION

marcobeer
Active Participant

Hello Symbat,

Here you go:

DATA: lc_string  TYPE C LENGTH 64 VALUE 'abba',
      lc_reverse TYPE C LENGTH 64,
      li_length  TYPE I VALUE -1.

DO.
  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string          = lc_string
      lang            = sy-langu
    IMPORTING
      rstring         = lc_reverse
    EXCEPTIONS
      too_small       = 1
      OTHERS          = 2.

  IF sy-subrc = 0 AND lc_string = lc_reverse. "Check if string matches reverse
    li_length = strlen( lc_string ) - 1.
    IF li_length >= 1.
      lc_string = lc_string(li_length). "Remove last character of string and try again
    ELSE.
      EXIT. "String too small -> all characters were the same
    ENDIF.
  ELSE.
    EXIT. "Strings do not match anymore in reverse
  ENDIF.
ENDDO.

IF li_length > 0.
  WRITE |Solution: | && lc_string.
ELSEIF li_length = 0.
  WRITE 'No solution (all characters are the same).'.
ELSE.
  WRITE 'No solution (no palindrome).'.
ENDIF

Let's just hope your teacher does not read on SCN and sees that you're letting others do your homework 😉

Best Regards
Marco

4 REPLIES 4

marcobeer
Active Participant

Hello Symbat,

Here you go:

DATA: lc_string  TYPE C LENGTH 64 VALUE 'abba',
      lc_reverse TYPE C LENGTH 64,
      li_length  TYPE I VALUE -1.

DO.
  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string          = lc_string
      lang            = sy-langu
    IMPORTING
      rstring         = lc_reverse
    EXCEPTIONS
      too_small       = 1
      OTHERS          = 2.

  IF sy-subrc = 0 AND lc_string = lc_reverse. "Check if string matches reverse
    li_length = strlen( lc_string ) - 1.
    IF li_length >= 1.
      lc_string = lc_string(li_length). "Remove last character of string and try again
    ELSE.
      EXIT. "String too small -> all characters were the same
    ENDIF.
  ELSE.
    EXIT. "Strings do not match anymore in reverse
  ENDIF.
ENDDO.

IF li_length > 0.
  WRITE |Solution: | && lc_string.
ELSEIF li_length = 0.
  WRITE 'No solution (all characters are the same).'.
ELSE.
  WRITE 'No solution (no palindrome).'.
ENDIF

Let's just hope your teacher does not read on SCN and sees that you're letting others do your homework 😉

Best Regards
Marco

0 Kudos

haha, I hope so too.

thank youu

marcobeer
Active Participant
0 Kudos

You‘re welcome, Symbat. Please do not forget to „Accept“ answers, if helpful. This helps others to find solutions easier, when searching for similar questions. Thank you.

Best Regards
Marco

matt
Active Contributor

Better to try to figure it out for youself than have others do it. it's the only way to really learn.

I'd in fact hope your teacher does find it. You're only cheating yourself.