In this program user will enter a sentence and the program will count upper case, lower case characters, digits and special characters in the sentence using Character class methods.
import java.io.*;
class Suddhashil
{
String str;
int i,len,ucase,lcase,digit,special;
char ch;
BufferedReader br;
Suddhashil()
{
ucase=lcase=digit=special=0;
br=new BufferedReader(new InputStreamReader(System.in));
}
public void takeString()throws Exception
{
System.out.println("Enter the sentence:");
str=br.readLine();
len=str.length();
for(i=0;i< len;i++)
ch=str.charAt(i);
if(Character.isUpperCase(ch))
ucase++;
else if(Character.isLowerCase(ch))
lcase++;
else if(Character.isDigit(ch))
digit++;
else
special++;
}
System.out.println("Upper Case Characters="+ucase);
System.out.println("Lower Case Characters="+lcase);
System.out.println("Digits="+digit);
System.out.println("Special Characters="+special);
}
public static void main(String args[]) throws Exception
{
obj.takeString();
}
}
Sample input and output
Enter the sentence:
this is suddhashil Chakraborty,mk.
Upper Case Characters=1
Lower Case Characters=28
Digits=0
Special Characters=5
Related Post: BlueJ Programs on String/Sentence
No comments:
Post a Comment