-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path086-Count-Words-in-a-String.java
More file actions
72 lines (55 loc) · 2.19 KB
/
Copy path086-Count-Words-in-a-String.java
File metadata and controls
72 lines (55 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* ============================================================================
* Program Name : Count Words in a String
* File Name : 086-Count-Words-in-a-String.java
* Class Name : CountWordsInAString
*
* Description:
* This program accepts a String from the user and counts the total
* number of words present in the String.
*
* Objective:
* - Understand how to read a String from the user.
* - Learn how to remove extra spaces using trim().
* - Learn how to split a String into words using split().
* - Count and display the total number of words.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import java.util.Scanner;
public class CountWordsInAString {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Create a Scanner object to read input from the keyboard.
Scanner scanner = new Scanner(System.in);
// Ask the user to enter a string.
System.out.print("Enter a String: ");
// Read the complete string entered by the user.
String text = scanner.nextLine();
// Remove leading and trailing spaces.
text = text.trim();
// Check whether the string is empty.
if (text.isEmpty()) {
// Display the result for an empty string.
System.out.println("Number of Words: 0");
} else {
// Split the string into words using one or more spaces.
String[] words = text.split("\\s+");
// Count the total number of words.
int wordCount = words.length;
// Display the entered string.
System.out.println("Entered String: " + text);
// Display the total number of words.
System.out.println("Number of Words: " + wordCount);
}
// Example Output:
// Enter a String: Java is a Programming Language
// Entered String: Java is a Programming Language
// Number of Words: 5
// Close the Scanner object to release system resources.
scanner.close();
}
}