Computer Applications
Write a program in Java to accept a String from the user. Pass the String to a method First(String str) which displays the first character of each word.
Sample Input : Understanding Computer Applications
Sample Output:
U
C
A
Answer
import java.util.Scanner;
public class KboatFirstCharacter
{
public void first(String str) {
String t = " " + str;
int len = t.length();
for (int i = 0; i < len - 1; i++) {
if (t.charAt(i) == ' ') {
char ch = t.charAt(i + 1);
System.out.println(ch);
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = in.nextLine();
KboatFirstCharacter obj = new KboatFirstCharacter();
obj.first(s);
}
}
Variable Description Table
Program Explanation
Output
Related Questions
Write a program to accept 10 numbers in a Single Dimensional Array. Pass the array to a method Search(int m[], int ns) to search the given number ns in the list of array elements. If the number is present, then display the message 'Number is present' otherwise, display 'number is not present'.
Write a program in Java to accept a String from the user. Pass the String to a method Change(String str) which displays the first character of each word after changing the case (lower to upper and vice versa).
Sample Input: Delhi public school
Sample Output:
d
P
SWrite a program in Java to accept the name of an employee and his/her annual income. Pass the name and the annual income to a method Tax(String name, int income) which displays the name of the employee and the income tax as per the given tariff:
Annual Income Income Tax Up to ₹2,50,000 No tax ₹2,50,001 to ₹5,00,000 10% of the income exceeding ₹2,50,000 ₹5,00,001 to ₹10,00,000 ₹30,000 + 20% of the amount exceeding ₹5,00,000 ₹10,00,001 and above ₹50,000 + 30% of the amount exceeding ₹10,00,000 Write a class with the name Area using method overloading that computes the area of a parallelogram, a rhombus and a trapezium.
Formula:
Area of a parallelogram (pg) = base * ht
Area of a rhombus (rh) = (1/2) * d1 * d2
(where, d1 and d2 are the diagonals)Area of a trapezium (tr) = (1/2) * ( a + b) * h
(where a and b are the parallel sides, h is the perpendicular distance between the parallel sides)