i.  - This program asks you to input a sentence and counts the number of  * letters and words in the sentence.  *   */ public class CountSentence {     public static void main(String[] args)     {         Scanner scan = new Scanner(System.in);         String sentence = "";         int words = 0;         int letters = 0;         int index = 0;         /** ADD CODE HERE             1. Ask the user to input a sentence assuming the sentence ends             with a full stop/period (and also assume that there are only letters (a - z)             and spaces in the sentence - no numbers).             2. Loop while the current character in the sentence is not equal to a full stop/period.                  2.1  If current character is a letter then add 1 to the number of letters                  otherwise character is a space and so 1 can be added to the number of words. Make                 sure you move past any spaces to start at next word.          */         System.out.println("The number of words are " + words);         System.out.println("The number of letters are " + letters);     }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter7: User-defined Simple Data Types, Namespaces, And The String Type
Section: Chapter Questions
Problem 3PE
icon
Related questions
Question

*i.  - This program asks you to input a sentence and counts the number of
 * letters and words in the sentence.
 * 
 */

public class CountSentence
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String sentence = "";
        int words = 0;
        int letters = 0;
        int index = 0;

        /** ADD CODE HERE

            1. Ask the user to input a sentence assuming the sentence ends
            with a full stop/period (and also assume that there are only letters (a - z)
            and spaces in the sentence - no numbers).

            2. Loop while the current character in the sentence is not equal to a full stop/period. 
                2.1  If current character is a letter then add 1 to the number of letters 
                otherwise character is a space and so 1 can be added to the number of words. Make
                sure you move past any spaces to start at next word.
         */

        System.out.println("The number of words are " + words);
        System.out.println("The number of letters are " + letters);
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
InputStream
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning