String

Count Uppercase, Lowercase,Special character and Numeric values in Java

Count Uppercase, Lowercase, Special character and Numeric values in Java

In this article, we will discuss the concept of the Count Uppercase, Lowercase, Special character and Numeric values in Java

In this post, we are going to learn how to count Uppercase, Lowercase, Special character and Numeric values of the given String in Java programming language

Count upper, lower, number, special character

Uppercase, Lowercase, Special character and Numeric values in Java

Find Uppercase, Lowercase, Special character and Numeric values using for loop

The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters of the given string using for loop in Java programing language

Program 1

import java.util.Scanner;
public class CountUpperLowerOthers{
public static void main(String args[]){
//variable declaration
    String str;
int upper=0,lower=0,num=0,Special=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'){//check uppercase
    upper++;
}
else if(ch>='a' && ch<='z'){ //check lower case
    lower++;
}
else if(ch>='0' && ch<='9'){  //check numeric
    num++;
}
else{
    Special++;
}

}
System.out.println("\nlowercase letters: "+lower);
System.out.println("uppercase letters: "+upper);
System.out.println("Number: "+num);
System.out.println("Special characters: "+Special);
}
}

When the above code is executed, it produces the following result

Enter the String
code4coding.com-Learn_Coding

lowercase letters: 22
uppercase letters: 2
Number: 1
Special characters: 3

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize some integer variables as int upper=0,lower=0,num=0,Special=0;
  3. The user asked to enter a string to count upper case, lower case, Numeric, Special characters
  4. The given string is stored in the variable str;
  5. A for-loop is used to count total characters of the given string
  6. It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
  7. Use an if statements to test  (ch>=’A’ && ch<=’Z’), if it is true,  The upper becomes upper + 1(upper=upper+1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true, The lower becomes lower + 1(lower=lower+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. If it is true The num becomes num+1(num=num+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the upper case, lower case, Numeric, Special characters of the given string

 

Find Uppercase, Lowercase, Special character and Numeric values using while loop

The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters of the given string using while loop in Java programing language

Program 2

import java.util.Scanner;
public class CountUpperLowerOthers1{
public static void main(String args[]){
//variable declaration
    String str;
int upper=0,lower=0,num=0,Special=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++;
}
else if(ch>='0' && ch<='9'){
    num++;
}
else{
    Special++;
}
 i++;
}
System.out.println("\nlowercase letters: "+lower);
System.out.println("uppercase letters: "+upper);
System.out.println("Number: "+num);
System.out.println("Special characters: "+Special);
}
}

When the above code is executed, it produces the following result

Enter the String
URL:code4coding.com

lowercase letters: 13
uppercase letters: 3
Number: 1
Special characters: 2

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize an integer variables as int upper=0,lower=0,num=0,Special=0;
  3. The user asked to enter a string to count upper case, lower case, Numeric, Special characters
  4. The given string is stored in the variable str;
  5. A while-loop is used to count total characters of the given string.
  6. It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
  7. Use an if condition to test (ch>=’A’ && ch<=’Z’), if it is true,  The upper becomes upper + 1(upper=upper+1);
  8. When it is false, control moves to the else-if part and evaluates the test expression of else-if . if it is true The lower becomes lower + 1(lower=lower+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the upper case, lower case, Numeric, Special characters of the given string

Find  Uppercase, Lowercase, Special character and Numeric values using do-while loop

The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters of the given string using do-while loop in Java programing language

Program 3

import java.util.Scanner;
public class CountUpperLowerOthers2{
public static void main(String args[]){
//variable declaration
    String str;
int upper=0,lower=0,num=0,Special=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;
do{
char ch=str.charAt(i);
if(ch>='A' && ch<='Z'){
    upper++;
}
else if(ch>='a' && ch<='z'){
    lower++;
}
else if(ch>='0' && ch<='9'){
    num++;
}
else{
    Special++;
}
 i++;
}while(i<str.length());
System.out.println("lowercase letters: "+lower);
System.out.println("uppercase letters: "+upper);
System.out.println("Number: "+num);
System.out.println("Special characters: "+Special);
}
}

When the above code is executed, it produces the following result

Enter the String
code4coding.com-Java,C,C++,Python
lowercase letters: 26
uppercase letters: 4
Number: 1
Special characters: 7

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize an integer variables as int upper=0,lower=0,num=0,Special=0;
  3. The user asked to enter a string to count upper case, lower case, Numeric, Special characters
  4. The given string is stored in the variable str;
  5. A do-while-loop is used to count total characters of the given string.
  6. It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
  7. Use an if condition to test if(ch>=’A’ && ch<=’Z’), if it is true,  The upper becomes upper + 1(upper=upper+1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The lower becomes lower + 1(lower=lower+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the upper case, lower case, Numeric, Special characters of the given string

 

Suggested for you

for loop in Java language

while loop in Java language

do-while loop in Java language

 

Similar post

C++ program to count the total number of characters in the given string

C program to count the total number of characters in the given string

Python program to count the total number of characters in the given string

C program to count the total number of characters in the given string including space

C++ program to count the total number of characters in the given string including space

Python program to count the total number of characters in the given string including space

Program to count the total number of characters in the given string with space in Python
Python code to Count Uppercase, Lowercase,Special character and Numeric values
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

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

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago