Alphabet

Program to count uppercase and lowercase letter of given string in Java

Program to count uppercase and lowercase letter of the given string in Java

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

Count upper case lower case

Code to count uppercase or lowercase letter of the given string

Java code to count uppercase or lowercase letter using for loop

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

  1. Declare a variable as String str;
  2. Declare and initialize two integer counter-variable as int upper=0 and lower=0;
  3. The user asked to enter a string to count upper case and lower case letters
  4. A for-loop is used to count the total number of upper case and lower case of the given string
  5. It is initialized as i=0, and checks condition whether i<str.length(); and executes the loop until the given condition becomes true
  6. Use an if statement to test upper case, if the test expression is true,  upper becomes upper + 1    (upper=upper+1)
  7. When the if-statements is false, The control moves to else if and checks the test expression of else-if
  8. If the test expression of else-if is true,  lower becomes lower + 1(lower =lower +1)
  9. If the test expression of else if is false, control terminates from the loop
  10. Finally, the program displays the number of upper case and lowercase letters of the given string.

 

Java code to count uppercase or lowercase letter using while loop

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

  1. Declare a variable as String str;
  2. Declare and initialize two integer counter-variable as int upper=0 and lower=0;
  3. The user asked to enter a string to count upper case and lower case letters
  4. A while-loop is used to count total upper case and lower case of the given string
  5. It is initialized as i=0, and checks condition whether i<str.length(); and executes the loop until the given condition becomes true
  6. Use an if statement to test upper case, if the test  expression is true,  upper becomes upper + 1    (upper=upper+1)
  7. When the if-statements is false, The control moves to else if and checks the test expression of else-if
  8. If the test expression of else-if is true,  lower becomes lower + 1(lower =lower +1)
  9. If the test expression of else if is false, control terminates from the loop
  10. Finally, the program displays the number of upper case and lowercase letters of the given string.

 

 

Java code to count uppercase or lowercase letter using do-while loop

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

  1. Declare a variable as String str;
  2. Declare and initialize two integer counter-variable as int upper=0 and lower=0;
  3. The user asked to enter a string to count upper case and lower case letters
  4. A do-while-loop is used to count total upper case and lower case of the given string
  5. It is initialized as i=0, and checks condition whether i<str.length(); and executes the loop until the given condition becomes true
  6. Use an if statement to test upper case, if the test  expression is true,  upper becomes upper + 1    (upper=upper+1)
  7. When the if-statements is false, The control moves to else if and checks the test expression of else-if
  8. If the test expression of else-if is true,  lower becomes lower + 1(lower =lower +1)
  9. If the test expression of else if is false, control terminates from the loop
  10. Finally, the program displays the number of upper case and lowercase letters of the given string.

 

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

 

 

 

C Program for count upper or lowercase letter of the given string
Python Program for count upper or lowercase letter of the given string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago