Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class StringDetails {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.print("Input a String: ");
- String str = in.nextLine();
- //if your remove this, spaces would be counted as special character
- str = str.replace(" ", "").trim();
- int noOfCharacters = str.length();
- int noOfVowels = 0;
- int noOfConsonants = 0;
- int noOfUpperCase = 0;
- int noOfLowerCase = 0;
- int noOfDigits = 0;
- int noOfSpecialCharacter = 0;
- for (char x :str.toCharArray()) {
- if(Character.isDigit(x)){
- noOfDigits++;
- }
- else if(Character.isAlphabetic(x)){
- if(Character.isUpperCase(x)){
- noOfUpperCase++;
- }else{
- noOfLowerCase++;
- }
- if(isVowel(x)){
- noOfVowels++;
- }else{
- noOfConsonants++;
- }
- }
- else{
- noOfSpecialCharacter++;
- }
- }
- System.out.println("Number of Characters: " + noOfCharacters);
- System.out.println("Number of Vowels: " + noOfVowels);
- System.out.println("Number of Consonants: " + noOfConsonants);
- System.out.println("Number of UPPERCASE: " + noOfUpperCase);
- System.out.println("Number of lowercase : " + noOfLowerCase);
- System.out.println("Number of Digits: " + noOfDigits);
- System.out.println("Number of Special Characters: " + noOfSpecialCharacter);
- }
- public static boolean isVowel(char c) {
- return "AEIOUaeiou".indexOf(c) != -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement