The document provides code for a Java program that reads a string from the user and prints each character of the string on its own line. It then provides a solution to modify the program to print the characters in reverse order down the column. The code defines a main method to get user input, and a count method to iterate through the string and return the number of occurrences of a given character.
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
1- Write a java program that reads a String from the user and prints o.docx
1. 1. Write a java program that reads a String from the user and prints out each individual character
from the String on it's own line in the command line (i.e. a column instead of a row. Use
System.out.println)
2. Do the same thing as in Assignment6_1, except print it in reverse (down the column).
Solution
A java program that reads a String from the user and prints out each individual character
from the String on it's own line in the command line
public static void main(String[] args) {
// ask the user to enter a string
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a string: ");
String s = input.nextLine();
// the user to enters a character
System.out.print("Enter a character: ");
char ch = input.nextLine().charAt(0);
int count = count(s, ch);
System.out.println(count);
}
public static int count(String str, char ch) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
count++;
}
}
return count;