Application Development and Automation 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: 
Read only

string

sushant_singh
Participant
0 Likes
1,026

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 .

7 REPLIES 7
Read only

Former Member
0 Likes
985

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

Read only

Former Member
0 Likes
985

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

Read only

Former Member
0 Likes
985

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.

Read only

Former Member
0 Likes
985

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

Read only

Former Member
0 Likes
985

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.

Read only

Former Member
0 Likes
985

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.

Read only

Former Member
0 Likes
985

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.