4: WAP to count the number of Alphabet/ Digit/ Special Character/ Blank Space And Words in the sentence

4: WAP to count the number of Alphabet/ Digit/ Special Character/ Blank Space And Words in the sentence

Also, you can use the Java compiler to compile a program.

SOURCE CODE:

import java.io.*;
class Space{
public static void main(String args[]) throws IOException
{
	String s; //Declaration os String varible to read the sentence
	BufferedReader r=new BufferedReader(new 	 
        InputStreamReader(System.in));//assigning new input method
	System.out.println("Enter the sentence"); //prompt the user to 
         enter the sentence
	s=r.readLine(); //Read the user entered sentence into variable 
	int i=1; //for count of loop
	int a=0; //For count of alphabet
	int d=0; //For count of digit
	int sc=0; //For count of special character
	int bs=0; //For count of blank spaces
	int w=1; //For count of words
	char ch; //For a single character of sentence
	int l=s.length(); //String length of sentence in variable l
	for(i=0;i<l;i++)
	{
		ch=s.charAt(i);
		if(Character.isLetter(ch) )
			++a;
		else
		if(Character.isDigit(ch))
			++d;
		else
		if(ch==' ')
			++bs;
		else
			++sc;
	}
	System.out.println("There are "+a+" Alphabet");
	System.out.println("There are "+d+" Digit");
	System.out.println("There are "+sc+" Special Character");
	System.out.println("There are "+bs+" Blank Spaces");
	for(i=0;i<l;i++)
	{
		if(s.charAt(i)==' ')
			w++;
	}
	System.out.println("There are "+w+" Word");
}//END OF MAIN
}//END OF CLASS

OUTPUT.

C:\A>javac Space.java
C:\A>java Space
Enter the sentence
January2018 #$ ,
There are 7 Alphabet
There are 4 Digit
There are 3 Special Character
There are 2 Blank Spaces
There are 3 Word

Also View: – Write a program to create a registration form using AWT.

Also View- Write a java program to create a window using swing.

1 thought on “4: WAP to count the number of Alphabet/ Digit/ Special Character/ Blank Space And Words in the sentence

  1. Your method of explaining the whole thing in this post is, in fact, pleasant, every one be capable of effortlessly be aware of it, Thanks a lot.

Leave a Reply