Question
Write program to input a sentence and convert it into uppercase and count and display the total no. of words starting with a letter ‘A’.
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING
Sample Output: Total number of word Starting with letter ‘A’= 4
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program8
{
public static void main(String[]args)
{
String sen="";
int i=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
sen=sc.nextLine();
sen=sen.toUpperCase();
int count=0;
for(i=0;i< sen.length();i++)
{
if(i==0 && sen.charAt(i)=='A')
{
count++;
}
else if(i>0)
{
if((sen.charAt(i - 1) == ' ') && (sen.charAt(i)=='A'))
{
count++;
}
}
}
System.out.println("Total number of words Starting with letter 'A'= "+count);
}
}