Write a Java Program to count the number of letters, numbers ,spaces and others in a string


import java.util.Scanner;

public class CountingProgram

{          

           static void count(String x){

                              char[] ch = x.toCharArray();

                              int letter = 0;

                              int space = 0;

                              int num = 0;

                              int other = 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{

                                             other ++;

                                      }

                              }

                              System.out.println("user input : "+x);

                              System.out.println("letter: " + letter);

                              System.out.println("space: " + space);

                              System.out.println("number: " + num);

                              System.out.println("other: " + other);

                              }

         public static void main(String[] args)

         {       Scanner s= new Scanner(System.in);

                  System.out.println("enter any number or letter");

                        String test = s.nextLine();

                    CountingProgram.count(test);

         }  

}


Output :

enter any number or letter

Darga blogs 715

user input : Darga blogs 715

letter: 10

space: 2

number: 3

other: 0

Explanation :

1.import java.util.Scanner;: This line imports the Scanner class from the java.util package, which is used to read input from the user.
 
2.The CountingProgram class is defined with the following methods:
 
3.The main method:
  • Creates a new Scanner object s to read input from the user.
  • Asks the user to enter a string: System.out.println("enter any number or letter");
  • Reads the user's input as a line of text using s.nextLine() and assigns it to the test variable.
  • Calls the count method of the CountingProgram class, passing the user's input as an argument.
  • count(String x): This method takes a string x as input and counts the occurrences of letters, spaces, digits, and other characters in the input string. It does so by converting the input string to a character array and then iterating through each character to check its type using various Character class methods like Character.isLetter, Character.isDigit, and Character.isSpaceChar.
4. Inside the count method
  • char[] ch = x.toCharArray();: Converts the input string x into a character array ch.
  • int letter = 0;, int space = 0;, int num = 0;, int other = 0;: Initialize counters for letters, spaces, digits, and other characters.
  • The for loop iterates through each character in the character array:
  •     if(Character.isLetter(ch[i])): Checks if the character is a letter.
  •     else if(Character.isDigit(ch[i])): Checks if the character is a digit.
  •     else if(Character.isSpaceChar(ch[i])): Checks if the character is a space.
  •     else: This block covers any character that doesn't fall into the previous categories.
After the loop, the program prints out the input string and the counts of different types of character

Comments

Popular posts from this blog

Installing MySQL and MySQL Workbench

Java Program to Check Palindrome Number

Scenario : 1