Write a program to input a sentence and convert it into uppercase and count and display the total number of words starting with a letter 'A'.
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.
Sample Output : Total words starting with letter 'A' = 4.
Solution:
import java.util.Scanner;
public class Year2019Q1
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = in.nextLine();
str = " " + str;
int count = 0;
int len = str.length();
str = str.toUpperCase();
for (int i = 0; i < len - 1; i++)
{
if (str.charAt(i) == ' ' && str.charAt(i + 1) == 'A')
{
count++;
}
}
System.out.println("Total words starting with letter 'A' = " + count);
}
}
Output:
No comments:
Post a Comment