write a program in java to accept a string from the user and print the number of uppercase, lowercase and digits present in the string
Answers
import java.util.Scanner;
public class StringContents {
public static void main(String[ ] args) {
System.out.print("Enter a string - ");
String sentence = new Scanner(System.in).nextLine( );
int countUpper = 0,
countLower = 0,
countDigit = 0;
for (char character : sentence.toCharArray( )) {
if (Character.isLetter(character))
if (Character.isUpperCase(character)) countUpper++;
else countLower++;
else if (Character.isDigit(character)) countDigit++;
}
System.out.printf("Lowercase characters - %d\n" +
"Uppercase characters - %d\n" +
"Digits - %d", countLower, countUpper, countDigit);
}
}