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: 

reverse of a string

Former Member
0 Kudos
694

i have been using the FM string_reverse for reversing a string, but itz throwing an error.

Can anyone explain it with an example.

Thanks in Advance.....

Rijish

17 REPLIES 17

anversha_s
Active Contributor
0 Kudos
213

hi rijish,

DATA : LOC_string(30) type c.

DATA : in_string(30) type c.

CALL FUNCTION 'STRING_REVERSE'

EXPORTING

STRING = IN_STRING

LANG = 'E'

IMPORTING

RSTRING = LOC_STRING

EXCEPTIONS

TOO_SMALL = 1

OTHERS = 2.

rgds

anver

if helepd mark points

Message was edited by: Anversha s

0 Kudos
213

Thanks Anversha,

Itz working fine.

Regards

Rijish

0 Kudos
213

hi,

shall we able to reverse a string without using string_ reverse function. and also i want to find all the position of particular character in a string

0 Kudos
213

yes u can do it without using FM.

U can find the character position using the offset.

For example see this program.

data : c(5) value 'ABCDE'.

data : l type i,i type i value -1.

data : str(5) .

l = strlen( c ).

do l times.

i = i + 1.

l = l - 1.

STRi(1) = Cl(1).

enddo.

write STR.

0 Kudos
213

thanks ram. we can find how many times a particular character occurs in string but shall we able to find each ones position. for ex. if banana is the string i want all position of 'a'. is it posible

0 Kudos
213

U just copy and paste the below code....

<b>This gives u the no. of ocuurances of a given character in the given string ...</b>

parameter :

p_str(30) type c, " Input string.

p_char type c. " Character to be searched.

data :

w_pos type i value 1, " Position.

w_search type i, " sy-subrc.

w_count type i,

w_index type i,

w_key type i,

w_arr1(30) type c,

w_arr2(30) type c.

if p_char EQ space.

message 'Enter a character to be searched...'(006) type 'I'.

exit.

endif. " If p_char EQ space.

write : 'The character '(001), p_char , 'occured'(002),/ .

do.

split p_str at space into w_arr1 w_arr2.

w_key = 0.

w_pos = 1.

while w_search eq 0.

search w_arr1 for p_char starting at w_pos.

if sy-subrc eq 0.

w_key = sy-fdpos.

w_pos = w_key + w_pos.

add w_pos to w_count.

write: /'at postion'(003), w_count.

add 1 to : w_pos, w_index.

else.

exit.

endif. " If sy-subrc eq 0.

w_search = sy-subrc.

endwhile. " While w_search eq 0.

if w_pos ne 1.

write : 'in the word'(004), w_arr1.

add 1 to w_count.

skip.

endif. " If w_pos ne 1.

w_count = w_count + strlen( w_arr1 ).

if w_arr2 EQ space.

exit.

endif. " If w_arr2 EQ space.

clear p_str.

move w_arr2 to p_str.

enddo. " Do.

skip.

write : / 'The total no. of occurances : '(005), w_index.

reward points if helpful...

sai ramesh.

0 Kudos
213

hi ,

how to reward points to you. where to add points.

0 Kudos
213

hi ,

for what tcode IMA11 is used. Shall we able to create reblica of that.

0 Kudos
213

Hi please check ur code its not providing exact output. .number of occurances is correct . but the positions in which they occur are not correct.

0 Kudos
213

DATA : TEXT(20) TYPE C VALUE 'BANANA',
            SRCH(1) TYPE C VALUE 'A',
            CMPARE(1),
            COUNT TYPE I,
            POS TYPE I,
            OCCURPOS TYPE I,
            LENGTH TYPE I.
DATA : BEGIN OF ITAB.
            OCCRPOS TYPE I,
           END OF ITAB. 
LENGTH = STRLEN( TEXT ).
DO LENGTH TIMES.
CMPARE = TEXT+POS(1).
IF COMPARE = SRCH.
COUNT = COUNT + 1.
OCCURPOS = POS + 1.
ITAB-OCCRPOS = OCCURPOS.
APPEND ITAB.
ENDIF.
POS = POS + 1.
ENDDO.
LOOP AT ITAB.
WRITE : / 'OCCURING POS ' , ITAB-OCCRPOS.
ENDLOOP.

REGARDS

SHIBA DUTTA

Message was edited by:

SHIBA DUTTA

0 Kudos
213

parameter :

p_str(30) type c, " Input string.

p_char type c. " Character to be searched.

data :

w_pos type i value 1, " Position.

w_count type i,

w_index type i.

w_count = strlen( p_str ).

w_pos = 0.

do w_count times.

if p_str+w_pos(1) eq p_char.

w_pos = w_pos + 1.

write: / w_pos.

w_pos = w_pos - 1.

w_index = w_index + 1.

endif.

w_pos = w_pos + 1.

enddo.

write : / 'The total no. of occurances : '(005), w_index.

0 Kudos
213

Hi thanks. Your coding is working fine. will you have any idea about transaction IMA11 for what it is used.

0 Kudos
213

hi thanks. your coding is working fine. can u tell me how to reward points

Former Member
0 Kudos
213

Hi,

report YXYZ.
parameter S(10) type C.
data S1(10) type C.

<b>call function 'STRING_REVERSE'
exporting
STRING = S
LANG = 'E'
importing
RSTRING = S1.</b>

if S = S1.
write 😕 S, 'is palindrom'.
else.
write 😕 S, 'is not palindrom'.
endif.

Regards

Sudheer

0 Kudos
213

Thanks Sudheer,

Itz working fine.

Regards

Rijish

anversha_s
Active Contributor
0 Kudos
213

HI RIJISH,

DID IT WORK

REGDS

ANVER

Former Member
0 Kudos
213

Hi,

U have to export the input string in STRING field and RSTRING will be imported to the output string u want and this will contain the reverse of the string or else u can go for looping and reversing.

Regards,

S.Ravi Kumar