Advertisement
icorrelate

StringDetails.java

Dec 12th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class StringDetails {
  4. public static void main(String[] args) {
  5. Scanner in = new Scanner(System.in);
  6. System.out.print("Input a String: ");
  7. String str = in.nextLine();
  8.  
  9. //if your remove this, spaces would be counted as special character
  10. str = str.replace(" ", "").trim();
  11.  
  12. int noOfCharacters = str.length();
  13. int noOfVowels = 0;
  14. int noOfConsonants = 0;
  15. int noOfUpperCase = 0;
  16. int noOfLowerCase = 0;
  17. int noOfDigits = 0;
  18. int noOfSpecialCharacter = 0;
  19. for (char x :str.toCharArray()) {
  20. if(Character.isDigit(x)){
  21. noOfDigits++;
  22. }
  23. else if(Character.isAlphabetic(x)){
  24. if(Character.isUpperCase(x)){
  25. noOfUpperCase++;
  26. }else{
  27. noOfLowerCase++;
  28. }
  29.  
  30. if(isVowel(x)){
  31. noOfVowels++;
  32. }else{
  33. noOfConsonants++;
  34. }
  35. }
  36. else{
  37. noOfSpecialCharacter++;
  38. }
  39. }
  40.  
  41. System.out.println("Number of Characters: " + noOfCharacters);
  42. System.out.println("Number of Vowels: " + noOfVowels);
  43. System.out.println("Number of Consonants: " + noOfConsonants);
  44. System.out.println("Number of UPPERCASE: " + noOfUpperCase);
  45. System.out.println("Number of lowercase : " + noOfLowerCase);
  46. System.out.println("Number of Digits: " + noOfDigits);
  47. System.out.println("Number of Special Characters: " + noOfSpecialCharacter);
  48. }
  49.  
  50. public static boolean isVowel(char c) {
  51. return "AEIOUaeiou".indexOf(c) != -1;
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement