Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

The string is : @TechStudy.org 123456

letter: 12

space: 1

number: 6

other: 2

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java programming language Basic programs with examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to print the ascii value of a... >>
<< Write a Java program to compare two numbers...