Home > Back-end >  Why character from character array, received from recursive function is not shown?
Why character from character array, received from recursive function is not shown?

Time:10-15

I am trying to learn recursion in Java. I am trying to print English alphabets from A to Z. But when I run the code, something undesirable is printed. What happened to my A-Z characters?

The code is given below:

public class Recursion_Maharaj {
    public static void main(String[] args) {
        System.out.println("Recursion...");
        char[] alphabet = alphabets('A');
        for (char c :alphabet) {
            System.out.println(c);
        }
    }

    static char[] alphabets(char S){
        int start = (int) S; // ASCII = 65
        int end = 'Z';// ASCII = 90
        char character;
        char[] ret = new char[26];
        if (start <= end){
            start  ;
            character = (char) start;
            ret = alphabets(character);
        }
        return ret;
    }
}

The output is some undesirable character.

What mistake I am making?

CodePudding user response:

Let's analyze your code. Let's start with the last invocation of method alphabets. The value of the method parameter, i.e. S, will be [ (which is the character after Z in the ASCII table), hence the if condition will be false, hence the method will return an array of 26 elements where each element is the null character, i.e. a character whose ASCII code is zero – because in this line of code:

char[] ret = new char[26];

Java will implicitly initialize each array element to zero.

So the last invocation returns an array of null characters to the second last invocation, which is this line of code:

ret = alphabets(character);

And therefore the second last invocation also returns an array of null characters – and so on until you return to the first invocation which returns the array of null characters in this line of your code:

char[] alphabet = alphabets('A');

So when you print the contents of alphabet, it prints what appears to be 26 empty lines. Actually it is printing 26 lines where each line displays a single, null character. If you change the for loop to the following, it will print 26 zeroes.

for (char c :alphabet) {
    System.out.println((int) c);
}

You need to make the array one of the parameters of method alphabets and in the body of method alphabets you assign a value to an element of that array. Actually, I suggest using a List rather than an array because a List, unlike an array, does not have a fixed size and you cannot know, before-hand, how many letters will be added to the List. If your initial invocation is changed to:

char[] alphabet = alphabets('Y');

then how many letters should be returned? Not 26, right?

Here is my rewrite of your code:
(Note that char is a primitive and you cannot have a List of primitives.)

import java.util.ArrayList;
import java.util.List;

public class Recursion_Maharaj {
    private static void getLetters(List<Character> letters, char letter) {
        if (letter <= 'Z') {
            letters.add(letter);
            getLetters(letters, (char) (letter   1));
        }
    }

    public static void main(String[] args) {
        System.out.println("Recursion...");
        List<Character> list = new ArrayList<>();
        getLetters(list, 'A');
        for (Character c : list) {
            System.out.println(c);
        }
    }
}

Running the above code prints the following:

Recursion...
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

And if I change the initial invocation to:

getLetters(list, 'Y');

then the output is:

Recursion...
Y
Z
  • Related