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 number of words starting with letter 'A' = 4.
Answers
/**************************************************************************************
Java program to input a sentence and convert it into uppercase and count and display
the total no. of words starting with a letter ‘A’.
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING
Sample Output: Total number of word Starting with letter ‘A’= 4
Code created by Mahnaz Hazra
**************************************************************************************/
import java.util.Scanner;
class CountLetterA
{
public static void main(String[]args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String str=kb.nextLine();
str=str.toUpperCase();
int count=0;
for(int i=0;i<str.length();i++)
{
if(i==0&&str . charAt(i)=='A')
count++;
if(i>0)
if((str . charAt(i - 1) == ' ')&&(str . charAt(i)=='A'))
count++;
}
System.out.println("Total number of words Starting with letter 'A'= "+count);
}
}
Answer:
import java.util.*;
class Upper
{
public static void main(String args[])
{
Scanner inp=new Scanner(System.in);
String s=new String();
System.out.println("Enter a line:");
s=inp.nextLine();
char c;
int hi=0;
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c>=65 && c<=90)
{
hi++;
}
}
System.out.println("Total number of words start with capital letters are :"+hi);
}
}
Explanation: