01-09-2007 4:57 AM
hi ,
in my program the requirement is to find out the no. of times a character is present in a string. how can i do this.
plz send the code.
thanks .
01-09-2007 5:01 AM
same thread----
data: var1(30) type c value 'ghghj#ghjgjgh#ghjghjg#ghjg#'.
data : totcnt type i,
cnt type i,
v type c,
n type i.
cnt = strlen( var1 ).
do cnt times.
move var1+n(1) to v.
if v eq '#'.
totcnt = totcnt + 1.
endif.
n = n + 1.
if n = cnt .
exit.
endif.
enddo.
write:/ 'No of #s', totcnt .
kishan negi
01-09-2007 5:02 AM
try like this
data : len type i,
count type i,
pos type i ,
val.
compute len = strlen( str1 ).
do len times.
val = str1+pos(1).
if val = <your required char>.
count = count + 1.
endif.
pos = pos + 1.
enddo.
regards
shiba dutta
01-09-2007 5:03 AM
DATA: L_NAME TYPE STRING.
DATA: TEMP TYPE STRING.
DATA: CNT TYPE I.
DATA: FL(1).
CLEAR.
WHILE FL <> 'X'.
SEARCH L_NAME FOR 'A'.
IF SY-SUBRC = 0.
SPLIT FILE AT 'A' INTO PATH FILE.
CNT = CNT + 1.
ELSE.
FL = 'X'.
ENDIF.
ENDWHILE.
WRITE: CNT.
01-09-2007 5:05 AM
Hi,
Check this example
DATA: v_string TYPE string.
DATA: v_index TYPE sy-index.
DATA: v_count TYPE sy-index.
DATA: v_char.
DATA: v_len TYPE int4.
String
v_string = 'AadfdAAA'.
Get the length
v_len = strlen( v_string ).
Calculate the times.
DO v_len TIMES.
v_index = sy-index - 1.
v_char = v_string+v_index(1).
IF v_char = 'A'.
v_count = v_count + 1.
ENDIF.
ENDDO.
WRITE: / 'Number of times A ',v_count.
**I am checking how many times A has been present in the string.
Thanks,
Naren
01-09-2007 5:06 AM
DATA: L_NAME TYPE STRING.
DATA: TEMP TYPE STRING.
DATA: FL(1).
DATA: CNT TYPE I.
TEMP = L_NAME.
WHILE FL <> 'X'.
SEARCH TEMP FOR 'A'.
IF SY-SUBRC = 0.
SPLIT FILE AT 'A' INTO L_NAME TEMP.
CNT = CNT + 1.
ELSE.
FL = 'X'.
ENDIF.
ENDWHILE.
WRITE: CNT.
01-09-2007 5:10 AM
use this demo code -
PARAMETERS : P_CHAR TYPE C LOWER CASE.
data str type string.
dATA: LEN TYPE I,
COUNT TYPE I,
PASS TYPE I VALUE '0'.
str = 'amita'.
CONDENSE STR.
LEN = STRLEN( STR ).
DO LEN TIMES.
IF P_CHAR = STR+pass(1).
COUNT = COUNT + 1.
ENDIF.
pass = pass + 1.
ENDDO.
01-09-2007 5:12 AM
hi try this
data : str(20) type c value 'ABABCASDAFERA',
cnt type i,
i type i ,
len type i,
val type c.
len = strlen( str ).
do len times.
val = str+i(1).
if val eq 'A'.
cnt = cnt + 1.
endif.
i = i + 1.
enddo.
write cnt.