06-08-2007 7:12 AM
Hi,
I need a progarm on string comparision.
>if the string are not identical we need to count the number of alphabets which are same in both the strings
>we need to write the alphabet with its respective count number
06-08-2007 8:44 AM
hi check it once
REPORT Z_SAMPLE.
data : string1 type string value 'Ram uh Rag',
string2 type string value 'Raghu'.
data: len type i,
len1 type i,
n type i,
m type i,
count type i.
if string1 eq string2.
write 'strings r identical'.
else.
len = strlen( string1 ).
len1 = strlen( string2 ).
n = 0.
count = 0.
while n < len.
m = 0.
while m < len1.
if string1n(1) = string2m(1).
count = count + 1.
write 😕 count,string1+n(1).
endif.
m = m + 1.
endwhile.
n = n + 1.
endwhile.
endif.
06-08-2007 8:56 AM
Hi suneetha
Here are string operations
Here are those
String Operations
Concatenate
Split
Shift
Replace
Translate
Offset
String length
String comparision
Concatenate
data : a(10),b(10),c(10),d(40).
A = Apple. B = Orange. C = Banana.
Concatenate A B C into D.
Write:/ d.
Concatenate A B C into D separated by /.
Write:/ d.
Split
data : a(10),b(10),c(10),d(40).
D = Apple/Orange/Banana.
Split d at / into a b c.
Write:/ a
/ b,
/ c.
Shift
Data : a(6) value ABCDEF.
Shift a.(by default shifts to left by one place)
write: / a. BCDEF
A = ABCDEF.
Shift a by 2 places.
Write:/ a. CDEF
Data : a(6) value ABCDEF.
Shift a right.
Write:/ a. ABCDE
A = ABCDEF.
Shift a right by 2 places.
Write:/ a. ABCD
Data : a(6) value ABCDEF.
Shift a up to C. Write:/ a. CDEF
a = ABCDEF.
Shift a circular. Write:/ a. BCDEFA
Replace
Data p(6) value ABCABC.
Replace ABC with DEF into p.
Write:/ p. DEFABC
(Replaces first occurrence only)
Translate
Data p(11) ABC ABC ABC.
Translate p using ADBECF.
WRITE:/ P. DEF DEF DEF
Offset
Data p(6) value ABCDEF, q(3).
q = p+2(3).
Write 😕 q. CDE
q = p+0(1).
Write:/ q. A
String Length.
Data: a(50) value PQRPQRPQRXYZ,
b type i.
b = strlen( a ). 15
String Comparison
Contains any ca
Contains only co
Contains string cs and
Contains pattern cp.
Contains any (ca) [ case sensitive ]
If SAP ca ABAP/4.
Write:/ True.
Else.
Write:/ False.
Endif.
True
Contains only (co) [ case sensitive ]
If SAP co ABAP/4.
Write:/ True.
Else.
Write:/ False.
Endif.
False
Contains string (cs) [ not case sensitive ]
If ABAP/4 cs Ab.
Write:/ True.
Else.
Write:/ False.
Endif.
True
Contains pattern (cp) [ not case sensitive ]
If ABAP/4 cp *aP++.
Write:/ True.
Else.
Write:/ False.
Endif.
True
check this sample code
DATA: V_CHAR(4).
DATA : v_string TYPE string.
DATA:BEGIN OF itab OCCURS 0,
matnr LIKE mara-matnr,
meins LIKE mara-meins,
mhdhb LIKE mara-mhdhb,
END OF itab.
V_CHAR = ITAB-MHDHB.
CONCATENATE itab-matnr v_maktx itab-meins V_CHAR
INTO v_string SEPARATED BY '|'.
<b>2nd program</b>
data: l_char(10) type c value '23.54',
l_pchar(8) type p decimals 2.
l_pchar = l_char.
if l_pchar gt 1.
write:/ 'Success'.
endif.
Reward all helpfull answers
Regards
Pavan
06-08-2007 8:56 AM
data: a type char20 value 'abcde',
b type char10 value 'as',
g type char1.
data: c type i,
d type i,
e TYPE i,
f type i.
c = STRLEN( a ).
d = STRLEN( b ).
do d times.
e = e + 1.
g = b+f(e).
f = f + 1.
do c TIMES.
if g ca a.
write / g.
else.
write 😕 'fail' .
endif.
enddo.
enddo.
try this code
regards,
venkatesh
06-08-2007 9:00 AM
Hi,
Take 2 Strings and put the values in those strings then
IF STRING1 CO STRING2.
Write:/ 'Both are same'.
ELSE.
Write:/ 'Both are not same'.
ENDIF.
<f1> CO <f2>
is true if <f1> contains only characters from <f2>. The comparison is case-sensitive. Trailing blanks are included. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of the first character of <f1> that does not occur in <f2> .
Regards
Sudheer
06-08-2007 9:10 AM
Hi Sudheer,
IF 'ABC' CO 'CBA'.
Write:/ 'Both are same'.
Sorry, I don't believe.
Please read more about striing comparisons:
<a href="http://help.sap.com/saphelp_erp2005vp/helpdata/en/fc/eb3516358411d1829f0000e829fbfe/content.htm">Comparisons Between Character Strings and Byte Strings</a>
Regards,
Clemens
06-08-2007 9:07 AM
TRY THIS
DATA : BEGIN OF ICHAR OCCURS 0,
ALPH,
COUNT TYPE I,
END OF ICHAR.
DATA : TEXT1(30) VALUE 'MATCHING STRING',
TEXT2(30) VALUE 'SEARCH STRING',
V_CHAR,
LEN TYPE I,
POS TYPE I.
CONDENSE TEXT1 NO-GAPS.
LEN = STRLEN( TEXT1 ).
DO LEN TIMES.
V_CHAR = TEXT1+POS(1).
IF TEXT2 CA V_CHAR.
READ TABLE ICHAR WITH KEY ALPH = V_CHAR.
ICHAR-COUNT = ICHAR-COUNT + 1.
IF SY-SUBRC = 0.
MODIFY ICHAR INDEX SY-TABIX.
ELSE.
ICHAR-ALPH = V_CHAR.
APPEND ICHAR.
ENDIF.
ENDIF.
POS = POS + 1.
ENDDO.
SORT ICHAR BY ALPH.
LOOP AT ICHAR.
WRITE : / ICHAR-ALPH, ICHAR-COUNT.
ENDLOOP.
REGARDS
SHIBA DUTTA