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: 

A program that displays the alphabet

d4xtian
Participant
0 Kudos

I Create a program that displays the alphabet in lowercase letters followed by a newline.

any insight will be appreciated
*&---------------------------------------------------------------------*
*& Report ZNJ_TERRE01
*&---------------------------------------------------------------------*
*& Description:
*&   This Program displays the alphabet in lowercase letters
*&   followed by a newline.
*&
*& Technical description:
*&    1 - We declare an input, i.e. a constant which contains
*&        the list of letters of the alphabet
*&    2 - We count the number of the alphabet
*&    3 - we make a loop as many times as the length of the input
*&    4 - We assign to gv-current_alphabet the first letter of input
*&    5 - We transform the gv_current_alphabet in lower case
*&    6 - We display the result with line break
*&    7 - We Increment the by 1 the variable to move to the next char of the alphabet
*&
*&---------------------------------------------------------------------*
*& Change log:
*& Date             Author        Action
*& 2022-09-22       S4HA13        CREATED
*&
*&---------------------------------------------------------------------*

REPORT ZNJ_TERRE01.


*---------------------------------*
* DECLARATIONS

constants gc_list_alphabet type string value 'abcdefghijklmnoPQRStuvwxyz'.

data :
      gv_current_alphabet type c,

      gv_const_offset type i,
      gv_list_alphabet_lenght type i.


*---------------------------------*
* MAIN LOGIC

" We count the number of character in the variable gv_listalphabet
gv_list_alphabet_lenght = strlen( gc_list_alphabet ) .

" We make a loop (Do) as many times as the length of the input
DO gv_list_alphabet_lenght TIMES.

" We assign to gv-current_alphabet the first letter of input
  gv_current_alphabet = gc_list_alphabet+gv_const_offset.

" We transform the gv_current_alphabet in lower case
    TRANSLATE gv_current_alphabet to LOWER CASE .

" We display the result with line break
    write : / gv_current_alphabet .

" We Increment the by 1 the variable to move to the next char of the alphabet
    gv_const_offset = gv_const_offset + 1 .

ENDDO.
10 REPLIES 10

Ryan-Crosby
Active Contributor

Insight into what exactly?

d4xtian
Participant
0 Kudos

about the code..

Sandra_Rossi
Active Contributor
0 Kudos

Are you asking what people think about the code, but you don't have any opinion yourself?

d4xtian
Participant
0 Kudos

It isn't a question, it is just what people think about the code.

thkolz
Contributor

Please don't do typos in variable names (gv_list_alphabet_lenght)!
Your code can be shortened.
The alphabet is stored in system variable sy-abcde.

" We count the number of character in the variable gv_listalphabet
DATA(v_alphabet_length) = strlen( sy-abcde ) .

" We make a loop (Do) as many times as the length of the input
DO v_alphabet_length TIMES.
DATA(v_index) = sy-index - 1. "As it starts with 1 it's needs to be decremented
WRITE:/ to_lower( sy-abcde+v_index(1) ).
ENDDO.

0 Kudos

Here i didn't want to use a system variable, Here, it was asked to use a variable, you just taught me that there is a system variable that contains the alphabet thank you

Nitish2027
Participant
0 Kudos

Hi fotso,

Your code looks good. Although if you are interested to do the same task by using new ABAP syntaxes, you can do something like this:

    DATA: lv_string TYPE string VALUE 'abcdefghijklmnoPQRStuvwxyz'.

    DATA(lv_len) = strlen( lv_string ).

    DATA(lv_result) = REDUCE string(
                            INIT result = ``
                            FOR i = 0 THEN i + 1 WHILE i < lv_len
                            NEXT result = result && lv_string+i(1) && cl_abap_char_utilities=>newline
                        ).
    cl_demo_output=>display( lv_result ).

Try this and let me know if you have any doubts or queries regarding any of the above syntaxes.

0 Kudos

Maybe I'm too old-fashioned, but I don't see the advantage of this new syntax.
The old one with DO..ENDDO, WHILE...ENDWHILE looks much more intuitive to me.

I really like new features like inline-declarations, pipes and other string options.
But I think that some other statements only complicate things which were already good before.

0 Kudos

Well it's just another way of doing the same task. Some people, specially beginners with ABAP these days might be more familiar with the newer ways of writing the code using new ABAP syntaxes.

But I completely agree with your point here, and as a matter of fact I tried to compare the performance of both the syntaxes and it turns out doing it via Do Endo has a much better performance in this particular scenario 😛

0 Kudos

Thanks you i ll check, In the meantime, i saw that you us Local Variable, and not Global Variable why ?

Regards.