In this article, we will discuss the concept of the Program to count uppercase and lowercase letter of the given string in Java
In this post, we are going to learn how to count upper and lower case letters in a given string in Java programming language
The program allows to enter a String and it counts and displays whether the number of upper case and lower case letters of the given string using for loop in Java language
Program 1
import java.util.Scanner; public class CountUpperLower{ public static void main(String args[]){ //variable declaration String str; int upper=0,lower=0;//vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); 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++; } } System.out.println("lowercase letters: "+lower); System.out.println("uppercase letters: "+upper); } }
When the above code is executed, it produces the following result
Enter the String CodeForCoding lowercase letters: 10 uppercase letters: 3
Approach
The program allows to enter a String and it counts and displays whether the number of upper case and lower case letters of the given string using while loop in Java language
Program 2
import java.util.Scanner; public class CountUpperLower1{ public static void main(String args[]){ //variable declaration String str; int upper=0,lower=0;//vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); int i=0; while( i<str.length()){ char ch=str.charAt(i); if(ch>='A' && ch<='Z'){ upper++; } else if(ch>='a' && ch<='z'){ lower++; } i++; } System.out.println("lowercase letters: "+lower); System.out.println("uppercase letters: "+upper); } }
When the above code is executed, it produces the following result
Enter the String Java Programming OOP lowercase letters: 13 uppercase letters: 5
Approach
The program allows to enter a String and it counts and displays whether the number of upper case and lower case letters of the given string using do-while loop in Java language
Program 3
import java.util.Scanner; public class CountUpperLower2{ public static void main(String args[]){ //variable declaration String str; int upper=0,lower=0;//vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter a String "); str=scan.nextLine(); int i=0; do{ char ch=str.charAt(i); if(ch>='A' && ch<='Z'){ upper++; } else if(ch>='a' && ch<='z'){ lower++; } i++; }while( i<str.length()); System.out.println("lowercase letters: "+lower); System.out.println("uppercase letters: "+upper); } }
When the above code is executed, it produces the following result
Enter the String Java And CPP lowercase letters: 5 uppercase letters: 5
Approach
Suggested for you
for loop in Java language
while loop in Java language
do-while loop in Java language
The operator in Java language
Data type and variable in Java language
Similar post
Java Program for count upper or lowercase letter of the given string
C Program for count upper or lowercase letter of the given string
Program for count upper or lowercase letter of the given string
Python Program for count upper or lowercase letter of the given string
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…