jeudi 7 mars 2019

prints a diamond shaped array based on the characters of the string

Write a program that reads a string and then prints a diamond shaped array based on the characters of the string. As an example, if the string has the value "SAMPLE",

Why my program is not printing a diamond-shaped pattern public static void main(String[] args) {

    System.out.println("Enter a String: ");
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    sc.close();
    int length = s.length();
    char[] a = s.toCharArray();

    for (int i = 0; i < length; i++) {
        for (int j = 0; j < i; j++)
            System.out.print(a[j]);
        System.out.println();
    }
    for (int i = length; i >= 0; i--) {
        for (int j = 0; j < i; j++)
            System.out.print(a[j]);
        System.out.println();
    }

}

}

Aucun commentaire:

Enregistrer un commentaire