Sunday, July 9, 2023

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'.

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

Check whether the word is Special word or Palindrome word.

Special words are those words which starts and ends with the same letter.  Examples: EXISTENCE COMIC WINDOW Palindrome words are those words...