Java Program to Count Letters, Digits, Spaces And Other Characters From A String

[24507 views]




Problem Statement:

Write a Java program to count the letters, spaces, numbers and other character of an input String.

Steps to solve this program:

  1. Take inputs.
  2. Apply for loop for each and every character checking process inside method.
  3. Count letter inside the string.
  4. Count numbers inside the string
  5. Count spaces and other characters inside the string.
  6. Call method inside main method
  7. Display all the counting variables

  1. Take Inputs
  2. Take all string inputs and initialize all the counting variable as 0.

    char[] ch = x.toCharArray(); int letter = 0; int space = 0; int num = 0; int otherchat = 0;

  3. Apply for loop for checking each and every character inside method:
  4. Using for loop from 0 to string length for iteration process inside count method.

    public static void count(String x) { for (int i = 0; i < x.length(); i++) { ... } }

  5. Count letter inside the string:
  6. Using if and isLetter() to count the total letters present inside string.

    if (Character.isLetter(ch[i])) { letter++; }

  7. Count digits inside the string:
  8. Using if and isDigit() to count the total digits present inside String.

    else if (Character.isDigit(ch[i])) { num++; }

  9. Count spaces and other characters inside the string:
  10. Using if and isSpaceChar() and else part is used to count the total spaces and other characters present inside string.

    else if (Character.isSpaceChar(ch[i])) { space++; } else { otherchat++; }

  11. Call count method inside main method:
  12. Inside main method call count(test) for displaying every count variables.

    public static void main(String[] args) { String test = "@Atechdaily.com 123456"; count(test); }

  13. Display all counting variables:
  14. After storing all values inside counting variable now print all the values inside method.

    System.out.println("letter: " + letter); System.out.println("space: " + space); System.out.println("number: " + num); System.out.println("other: " + otherchat);

Full Java Code:

import java.util.Scanner; public class Javaexcercise { public static void main(String[] args) { String test = "@Atechdaily.com 123456"; count(test); } public static void count(String x) { char[] ch = x.toCharArray(); int letter = 0; int space = 0; int num = 0; int otherchat = 0; for (int i = 0; i < x.length(); i++) { if (Character.isLetter(ch[i])) { letter++; } else if (Character.isDigit(ch[i])) { num++; } else if (Character.isSpaceChar(ch[i])) { space++; } else { otherchat++; } } System.out.println("The string is :"+x); System.out.println("letter: " + letter); System.out.println("space: " + space); System.out.println("number: " + num); System.out.println("other: " + otherchat); } }

Console Output

The string is :@Atechdaily.com 123456 letter: 13 space: 1 number: 6 other: 2
                 



Want to Clear Java Interview? Read Java Interview Notes prepared by Us


Are You Good enough in Java? Prove it!



Comments





Search
Get Answers to your Programming Questions


Recommended Deals ends in








Quiz For You:

Search Tags

    Count Letters in a String using Java

    Count Spaces in a String using Java