Write a Java program to count the letters, spaces, numbers and other characters of an input string

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
public class Javaexcercise {
 
 public static void main(String[] args) {
        String test = "@TechStudy.org 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 : @TechStudy.org 123456");
        System.out.println("letter: " + letter);
        System.out.println("space: " + space);
        System.out.println("number: " + num);
        System.out.println("other: " + otherchat);
            }
}

Result

Write a Java program to count the letters, spaces, numbers and other characters of an input string
Write a Java program to count the letters, spaces, numbers and other characters of an input string

Leave a Comment