Skip to Content

string

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 .

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

7 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05:01 AM

    same thread----

    find-number-of-occurances-of-a-particular-characte

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05: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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05: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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05: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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05: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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05: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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 05: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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.