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: 

program for string palindrome

Former Member
0 Kudos

hi,

can anyone send me the program for palindrome for a string and also for numbers..

its urgent

thankyou,

11 REPLIES 11

Former Member
0 Kudos

Check out this related thread for answer

Former Member
0 Kudos

Hi,

This is a bit of a broad spec!

Do you want a program to generate palindromes?

Or do you want it to check if a string is a palindrome?

Does it need to check if they are proper words?

And if so what language?

Regards,

Nick

P.S. I don't have such a program, and if I needed one I don't think ABAP would be my first choice of language

0 Kudos

i would like to check whether the string is palindrome or not? and i would like to use ABAP to do this,... do u have any program? if so send it to me for my reference.

matt
Active Contributor

Here's pseudo code - easy to convert to ABAP

pos_end = length of string
pos_start = 1
do pos_end/2 times.
  read character of string at position pos_start and pos_end.
  not the same?
    set flag
    exit
  add 1 to pos_start
  subtract 1 from pos_end
enddo.

if flag set
  say 'not palindrome'.
else
  say 'palindrome'

matt
Active Contributor
0 Kudos

Homework question is it?

matt

former_member230674
Contributor
0 Kudos

hai

sarvesh,

This is one of the logic to find the palendrom.

1. Find the Length of Given string using strlen.

2. Take n = 1 . len = strlen ( string).

len1 = len.

3. Using a Loop based on condition: n le len / 2.

3.1 Check if a[n] = a[len1].

3.2 if true

flag = 'y'.

else.

flag = 'n'.

endif.

3.3 n = n + 1. len1 = len1 - 1.

4. After the end of loop

if flag = 'y'.

write 'palendrom'.

else.

write ' not palendrom'.

endif.

I hope that, this is very simple logic and is very easy to convrt into program.

if useful, reward points.

Thank you,

G.V.K.Prasad

0 Kudos

Hey - same logic as mine.

SUMIT1
Product and Topic Expert
Product and Topic Expert
0 Kudos

Heii,

I too was going through the same Homework stuff....wanted to make it more lucid...as used in the other languages...i guess using the string reverse is a better option...but just to make it fundamental...here is the coding I did...

parameters: p_input type string.

data: len type i,

       len2 type i,

       counter type i,

       index type i.

len = strlen( p_input ).

len2 len - 1.

do len times.

   if p_input+index(1) = p_input+len2(1).

     counter = 1.

   else.

     counter = 0.

   endif.

   len2 = len2 - 1.

   index = index + 1.

enddo.

if counter = 1.

   write:/ 'Palindrome Number'.

else.

   write:/ 'Not a palindrome number'.

endif.

The results are accurate and tested.

Sumit

0 Kudos

you have to add exit statement below "counter = 0."

Dhivya
Active Participant
0 Kudos

Hi,

  

DATA : in(50) TYPE c,
       rev(50) TYPE c.

PARAMETERS  Input TYPE string .

in = input .

CONDENSE IN .


CALL FUNCTION 'STRING_REVERSE'
  EXPORTING
    string          = in
    lang            = sy-langu
IMPORTING
   RSTRING         = rev
* EXCEPTIONS
*   TOO_SMALL       = 1
*   OTHERS          = 2
          .

IF in EQ rev.

  WRITE ' THE STRING IS A PALINDROME ' .

ELSE .

  WRITE : ' THE STRING IS NOT A PALINDROME ' .

ENDIF.

Thanks,

Dhivya.

0 Kudos

"Program to check a palindrome string

class palindrome DEFINITION.

public SECTION.
class-methods: is_palindrome IMPORTING value(im_number) type String
RETURNING VALUE(result) type string.


endclass.

class PALINDROME IMPLEMENTATION.
method IS_PALINDROME.
"Check how many digits
Data(lv_lenght_number) = strlen( im_number ).
data(lv_count_loop) = lv_lenght_number div 2.
subtract 1 from lv_lenght_number.
"you can not use offset directly on importing parameter
data(lv_number) = im_number.
data(lv_counter) = 0.
while lv_count_loop ne 0.
data(lv_digit_from_left) = lv_number+lv_counter(1).
data(lv_digit_from_right) = lv_number+lv_lenght_number(1).
if lv_digit_from_left eq lv_digit_from_right.
add 1 to lv_counter.
subtract 1 from lv_lenght_number.
else.
result = 'Not a palindrome'.
exit.
endif.
result = 'Yes,it a palindrome'.
subtract 1 from lv_count_loop.
endwhile.

endmethod.
endclass.

START-OF-SELECTION.
data : result type string.
palindrome=>IS_PALINDROME(
EXPORTING
IM_NUMBER = 'abababa'
RECEIVING
result = result

).