Java: Count the letters, spaces, numbers and other characters of an input string
Java Basic: Exercise-38 with Solution
Count Characters in String
Write a Java program to count letters, spaces, numbers and other characters in an input string.
Test Data:
The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33
Pictorial Presentation:
Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise38 {
public static void main(String[] args) {
// Define a test string containing letters, numbers, spaces, and other characters
String test = "Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33";
// Call the count method to analyze the characters in the test string
count(test);
}
public static void count(String x) {
// Convert the input string to a character array
char[] ch = x.toCharArray();
// Initialize counters for letters, spaces, numbers, and other characters
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
// Iterate through the character array to categorize characters
for (int i = 0; i < x.length(); i++) {
// Check if the character is a letter
if (Character.isLetter(ch[i])) {
letter++;
}
// Check if the character is a digit (number)
else if (Character.isDigit(ch[i])) {
num++;
}
// Check if the character is a space
else if (Character.isSpaceChar(ch[i])) {
space++;
}
// Character falls into the "other" category if none of the above conditions are met
else {
other++;
}
}
// Display the original string
System.out.println("The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33");
// Display the counts of letters, spaces, numbers, and other characters
System.out.println("letter: " + letter);
System.out.println("space: " + space);
System.out.println("number: " + num);
System.out.println("other: " + other);
}
}
Explanation:
In the exercise above -
- The string to be analyzed is stored in the 'test' variable.
- The "count()" method takes a string 'x' as its parameter.
- Inside the "count()" method, the "toCharArray()" method is used to convert the input string x into an array of characters 'ch'.
- Four variables (letter, space, num, and other) are initialized to zero. These variables will be used to count the respective types of characters in the string.
- A for loop iterates through each character in the 'ch' array.
- Inside the loop, it checks the type of each character using various "Character" class methods like isLetter(), isDigit(), and isSpaceChar(). Depending on the type, it increments the corresponding counter variable (letter, num, space, or other).
- Finally, it prints the original string and the counts of each character type.
Sample Output:
The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33 letter: 23 space: 9 number: 10 other: 6
Flowchart:
Java Code Editor:
Previous: Write a Java program to reverse a string.
Next: Write a Java program to create and display unique three-digit number using 1, 2, 3, 4. Also count how many three-digit numbers are there.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/basic/java-basic-exercise-38.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics