Skip to content
October 7, 2013 / TDS Software

Java Q&A

Write a java program to get a string as a key board input. Then count the number of letters,
digits and spaces available in that string and display the count of letters, digits and spaces.
Save your answer as “Question10.java”.

 

import java.io.*;

class Question10{
public static void main(String[] args) {
String inp =””;
try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
System.out.print(“Enter a String : “);
inp = bf.readLine();

}catch (IOException e){
e.printStackTrace();
}

int letrs = 0 , digits = 0, spaces = 0;
for (int i = 0 ; i < inp.length(); i++){
char a = inp.charAt(i);
if (Character.isSpaceChar(a)){
spaces++;
}else if(Character.isDigit(a)){
digits++;
}else if (Character.isLetter(a)){
letrs++;
}
}

System.out.println(“Number of Letters : ” + letrs);
System.out.println(“Number of Digits : ” + digits);
System.out.println(“Number of Spaces : ” + spaces);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: