mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-23 12:35:55 +08:00
docs(Others): update countwords.java and crc32.java
- By @yanglbme
This commit is contained in:
@ -1,26 +1,26 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* You enter a string into this program, and it will return how
|
||||
* many words were in that particular string
|
||||
*
|
||||
* @author Marcus
|
||||
* You enter a string into this program, and it will return how many words were
|
||||
* in that particular string
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class countwords{
|
||||
public class CountWords {
|
||||
|
||||
public static void main(String[] args){
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.println("Enter your text: ");
|
||||
String str = input.nextLine();
|
||||
|
||||
System.out.println("Your text has " + wordCount(str) + " word(s)");
|
||||
input.close();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.println("Enter your text: ");
|
||||
String str = input.nextLine();
|
||||
|
||||
private static int wordCount(String s){
|
||||
if(s.isEmpty() || s == null) return 0;
|
||||
return s.trim().split("[\\s]+").length;
|
||||
}
|
||||
|
||||
System.out.println("Your text has " + wordCount(str) + " word(s)");
|
||||
input.close();
|
||||
}
|
||||
|
||||
private static int wordCount(String s) {
|
||||
if (s == null || s.isEmpty())
|
||||
return 0;
|
||||
return s.trim().split("[\\s]+").length;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user