Untitled

 avatar
unknown
plain_text
4 years ago
1.6 kB
4
Indexable
import java.util.*;

class Main {

  public static void part1(String s){

// we traverse through each character of the string and print it while padding an extra space in front of it

    for(int i=0; i<s.length(); i++){
      System.out.print(s.charAt(i)+" ");
    }
  }

  public static void part2(String s){

// we call part1 function to print the one liner pattern and then print all the characters of the string line by line except first and last char

    part1(s);
    System.out.println();
    for(int i=1; i<s.length()-1; i++){
      System.out.println(s.charAt(i));
    }
  }

  public static void part3(String s){


// we call boht part1 and part2 to print first two patterns

    System.out.println("\nExecuting part1:\n");
    part1(s);
    System.out.println("\n\nExecuting part2:\n");
    part2(s);
    System.out.println("\nExecuting part3:\n");

// we call pattern 2 as it prints a part of pattern required for 3

    part2(s);

// we print the string in reverse order to complete the third pattern

    for(int i=s.length()-1; i>=0; i--){
      System.out.print(s.charAt(i)+" ");
    }
  }

  public static void main(String[] args) {
    System.out.print("Enter a string: "); // asking user for input
    Scanner sc = new Scanner(System.in); // creating object of scanner class to scan user input
    String st = sc.nextLine();  // taking user input
    String s = st.toUpperCase();    // converting the user input string to upper case
    part3(s);   // calling part3 function to print all 3 patterns
  }
}
Editor is loading...