How to count uppercase and lowercase letters in a string?

yo, so I'm trying to make a program that can accept string input from the user, for example: "ONCE UPON a time", and then reports how many upper and lower case letters the string contains:

Output example

: the string contains 8 uppercase letters, the string has 5 lowercase letters, and they are supposed to use a string class, not arrays, any tips on how to get started with this? Thanks in advance, here is what I have done so far: D!

import java.util.Scanner; public class q36{ public static void main(String args[]){ Scanner keyboard = new Scanner(System.in); System.out.println("Give a string "); String input=keyboard.nextLine(); int lengde = input.length(); System.out.println("String: " + input + "\t " + "lengde:"+ lengde); for(int i=0; i<lengde;i++) { if(Character.isUpperCase(CharAt(i))){ } } } } 
+6
source share
6 answers

Just create counters that increment when lowercase or uppercase letters are found, for example:

 for (int k = 0; k < input.length(); k++) { /** * The methods isUpperCase(char ch) and isLowerCase(char ch) of the Character * class are static so we use the Class.method() format; the charAt(int index) * method of the String class is an instance method, so the instance, which, * in this case, is the variable `input`, needs to be used to call the method. **/ // Check for uppercase letters. if (Character.isUpperCase(input.charAt(k))) upperCase++; // Check for lowercase letters. if (Character.isLowerCase(input.charAt(k))) lowerCase++; } System.out.printf("There are %d uppercase letters and %d lowercase letters.",upperCase,lowerCase); 
+12
source

You can try the following code:

 public class ASCII_Demo { public static void main(String[] args) { String str = "ONCE UPON a time"; char ch; int uppercase=0,lowercase=0; for(int i=0;i<str.length();i++) { ch = str.charAt(i); int asciivalue = (int)ch; if(asciivalue >=65 && asciivalue <=90){ uppercase++; } else if(asciivalue >=97 && asciivalue <=122){ lowercase++; } } System.out.println("No of lowercase letter : " + lowercase); System.out.println("No of uppercase letter : " + uppercase); } } 
+1
source

Solution in Java8:

 private static long countUpperCase(String s) { return s.codePoints().filter(c-> c>='A' && c<='Z').count(); } private static long countLowerCase(String s) { return s.codePoints().filter(c-> c>='a' && c<='z').count(); } 
+1
source

java 8

 private static long countUpperCase(String inputString) { return inputString.chars().filter((s)->Character.isUpperCase(s)).count(); } private static long countLowerCase(String inputString) { return inputString.chars().filter((s)->Character.isLowerCase(s)).count(); } 
+1
source

Use regular expressions:

 public Counts count(String str) { Counts counts = new Counts(); counts.setUpperCases(str.split("(?=[AZ])").length - 1)); counts.setLowerCases(str.split("(?=[az])").length - 1)); return counts; } 
0
source

You simply iterate over the contents and use the Character functions to verify it. I use real code points, so it supports extra Unicode characters.

When working with code points, the index cannot simply increase by one, as some code points actually read two characters (aka code units). This is why I use while and Character.charCount(int cp) .

 /** Method counts and prints number of lower/uppercase codepoints. */ static void countCharacterClasses(String input) { int upper = 0; int lower = 0; int other = 0; // index counts from 0 till end of string length int index = 0; while(index < input.length()) { // we get the unicode code point at index // this is the character at index-th position (but fits only in an int) int cp = input.codePointAt(index); // we increment index by 1 or 2, depending if cp fits in single char index += Character.charCount(cp); // the type of the codepoint is the character class int type = Character.getType(cp); // we care only about the character class for lower & uppercase letters switch(type) { case Character.UPPERCASE_LETTER: upper++; break; case Character.LOWERCASE_LETTER: lower++; break; default: other++; } } System.out.printf("Input has %d upper, %d lower and %d other codepoints%n", upper, lower, other); } 

For this example, the result will be:

 // test with plain letters, numbers and international chars: countCharacterClasses("AABBร„รคoรŸabc0\uD801\uDC00"); // U+10400 "DESERET CAPITAL LETTER LONG I" is 2 char UTF16: D801 DC00 Input has 6 upper, 6 lower and 1 other codepoints 

It counts the German font as lowercase (no uppercase option) and special padding code (which is two code units / char long) as uppercase. The number will be considered "different."

Using Character.getType(int cp) instead of Character.isUpperCase() has the advantage that it only needs to look at the code point once for several (all) character classes. It can also be used to count all different classes (letters, spaces, controls, and all other unicode classes (TITLECASE_LETTER, etc.).

For a good background, read why you need to care about code points and units, check out: http://www.joelonsoftware.com/articles/Unicode.html

-1
source

Source: https://habr.com/ru/post/973602/


All Articles