count the number of words
Topic:
Problem description
Write a program, enter a string (not more than 80 in length), and then count how many words are included in the string.
For example: The string "This was a book" contains 4 words.
Input format: Enter a string that consists of a number of words separated by a space between the words.
Output format: Outputs an integer that is the number of words.
Input/Output sample
Sample user input data:
This was a book
The system output data is as follows:
4
Code
Import java.util.*;
public class Dancigeshutongji {
public static void Fun (String a)
{
System.out.print (A.split ("\\s+"). length);
where \\s denotes whitespace, carriage return, newline, and other whitespace characters,
The split method in string is based on regular expressions to check the points.
}
public static void Main (string[] args)
{
Scanner in =new Scanner (system.in);
String A=in.nextline ();
Fun (a);
}
}
Operation Result:
Count the number of words