
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Upper and Lower Case Characters in a Given String in Java
In order to count upper and lower case character we have to first find weather a given character is in upper case or in lower case. For this we would take concept of ASCII value of each character in Java.
In Java as we know for each character has corresponding ASCII value so we would compare each character that it lies in the range of upper case or in lower case.
Counting upper and lower case characters in a string in Java
In below example first convert string to a character array for easy transverse,then find weather it lies in upper case or lower case range also setted counter for upper case and lower case which get increased as character lies accordingly.
Example
public class CountUpperLower { public static void main(String[] args) { String str1 = "AbRtt"; int upperCase = 0; int lowerCase = 0; char[] ch = str1.toCharArray(); for(char chh : ch) { if(chh >='A' && chh <='Z') { upperCase++; } else if (chh >= 'a' && chh <= 'z') { lowerCase++; } else { continue; } } System.out.println("Count of Uppercase letter/s is/are " + upperCase + " and of Lowercase letter/s is/are " + lowerCase); } }
Output
Count of Uppercase letter/s is/are 2 and of Lowercase letter/s is/are 3
Advertisements