Given a line of text as input, output the number of characters excluding spaces, periods, or commas.
ex: if the input is:
a) listen,
b) mr. jones,
c) calm down.
the output is:
21
note: account for all characters that aren't spaces, periods, or commas (ex: "r", "2", "! ").

Respuesta :

ijeggs

Answer:

import java.util.Scanner;

public class ANot {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println("Please enter a string of words");

       String n = in.nextLine();

       int count = 0;

       for (int i=0; i<n.length(); i++){

           if((n.charAt(i) != '.') &&(n.charAt(i) != ',') && (n.charAt(i) != ' ')){

               count++;

           }

       }

       System.out.println(count);

   }

}

Explanation:

  1. Prompt User to input the string and assign to a variable
  2. SET count =0.
  3. Using a for loop iterate over the string
  4.  if((n.charAt(i) != '.') &&(n.charAt(i) != ',') && (n.charAt(i) != ' ')) then count =count +1.
  5.     PRINT count.