Write a program to count of uppercase and lowercase and their difference


Program
import java.util.Scanner; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the String "); String str = sc.next(); int strlen = str.length(); for(int i =0; i < str.length() ; i++){ char ch = str.charAt(i); if(!(ch >='a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')){ System.out.println(str + " is an invalid String"); return; } } if(strlen > 0 && strlen <= 10){ int upper = 0, lower = 0, ans = 0; for(int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch >= 'A' && ch <= 'Z') upper++; else if(ch >= 'a' && ch <= 'z') lower++; } ans = upper - lower; System.out.println("count of uppercase is :"+ upper); System.out.println("count of lowercase is :"+ lower); System.out.println("Hence the ans is :"+ ans); } else { System.out.println(str + " is an invalid String"); } } }
Input
Output