Write a menu driven program to display the pattern of a string entered by the user. If the user enters 'F' then it displays all the first characters and 'L' displays all the last characters of the String.
Eg: HONESTY IS THE BEST POLICY
F H L Y
I S
T E
B T
P Y
Answers
Answer:
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 for pattern 1");
System.out.println("Enter 2 for Pattern 2");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch (choice) {
case 1:
for (int i = 69; i >= 65; i--) {
for (int j = 65; j <= i; j++) {
System.out.print((char)j);
}
System.out.println();
}
break;
case 2:
String word = "BLUE";
int len = word.length();
for(int i = 0; i < len; i++) {
for(int j = 0; j <= i; j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
break;
default:
System.out.println("Incorrect choice");
break;
Explanation:
Hope it's helpful to u.
Mark me as brainlist if it is helpful to u
Answer:
import java.util.*;
public class prog_21{
public static void main(String args[]){
Scanner sc = new Scanner (System.in);
//Declaring all the variables here,
char ch,ch1;String s1="";
System.out.println("Enter the sentence in Upper Case : ");
String s = sc.nextLine();
System.out.println("****MENU****");
System.out.println("Enter 'F' for displaying the first letter of each of the words");
System.out.println("Enter 'L' for displaying the last letter of each of the words");
System.out.println("Enter your choice: ");
ch = sc.next().charAt(0);
switch(ch){
case 'F' :
System.out.println("Sample Output: ");
s=' '+s;
for(int i = 0;i<s.length();i++){
ch1=s.charAt(i);
if(ch1==' ')
System.out.println(s.charAt(i+1));
}
break;
case 'L':
System.out.println("Sample Output: ");
s=s+' ';
for(int i = 0;i<s.length();i++){
ch1=s.charAt(i);
if(ch1==' '){
System.out.println(s.charAt(i-1));
}
}
break;
default:
System.out.println("Wrong Input ! ");
}
}
}
Explanation: