How to enter a string in Java using Scanner class and count its special characters?
In this Tutorial we are going to learn How to enter a string in Java using Scanner class and count its special characters?
Program :-(How to enter a string in Java using Scanner class and count its special characters?)
import java.util.*;
class Count
{
Scanner sc=new Scanner(System.in);
String str;
int len;
int cnt,alpha,digit;
void getInput()
{
System.out.println("Enter Any String with Special Characters :");
str=sc.next();
len=str.length();
}
void show()
{
for(int i=0;i<len;i++)
{
char ch=str.charAt(i);
if(ch>='a'&& ch<='z'||ch>='A'&& ch<='Z')
{
alpha++;
}
else if(ch>='0' && ch<='9')
{
digit++;
}
else
{
cnt++;
}
}
System.out.println("Alphabets are :"+alpha);
System.out.println("Digits are :"+digit);
System.out.println("Special Symbols are :"+cnt);
}
public static void main(String args[])
{
Count c=new Count();
c.getInput();
c.show();
}
}
Output :-
Comments
Post a Comment