diff --git a/core-java-modules/core-java-string-operations-8/README.md b/core-java-modules/core-java-string-operations-8/README.md index 2dce44d217..7d843af9ea 100644 --- a/core-java-modules/core-java-string-operations-8/README.md +++ b/core-java-modules/core-java-string-operations-8/README.md @@ -1,2 +1 @@ - -### Relevant Articles: +### Relevant Articles: diff --git a/core-java-modules/core-java-string-operations-8/pom.xml b/core-java-modules/core-java-string-operations-8/pom.xml index 2495abccf8..a2f97a93d9 100644 --- a/core-java-modules/core-java-string-operations-8/pom.xml +++ b/core-java-modules/core-java-string-operations-8/pom.xml @@ -1,72 +1,34 @@ - - - 4.0.0 - core-java-string-operations-8 - core-java-string-operations-8 - jar - - - com.baeldung.core-java-modules - core-java-modules - 0.0.1-SNAPSHOT - - - - - org.apache.commons - commons-lang3 - ${apache.commons.lang3.version} - - - org.apache.commons - commons-text - ${commons-text.version} - - - org.liquibase - liquibase-core - 4.9.1 - test - - - org.junit.jupiter - junit-jupiter - 5.8.1 - test - - - org.liquibase - liquibase-core - 4.9.1 - test - - - junit - junit - 4.13.2 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${maven.compiler.source} - ${maven.compiler.target} - - - - - - - 11 - 11 - 3.13.0 - 1.10.0 - - - \ No newline at end of file + + + 4.0.0 + core-java-string-operations-8 + core-java-string-operations-8 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + 11 + 11 + + + diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java new file mode 100644 index 0000000000..64e8b7ebbd --- /dev/null +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java @@ -0,0 +1,90 @@ +package com.baeldung.countupperandlowercasechars; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class CountUpperAndLowercaseCharsUnitTest { + private static final String MY_STRING = "Hi, Welcome to Baeldung! Let's count letters!"; + + @Test + void whenUsingCountByCharacterRange_thenGetExpectedResult() { + LetterCount result = LetterCount.countByCharacterRange(MY_STRING); + assertEquals(4, result.getUppercaseCount()); + assertEquals(31, result.getLowercaseCount()); + } + + @Test + void whenUsingCountByCharacterIsLowerOrUpperCase_thenGetExpectedResult() { + LetterCount result = LetterCount.countByCharacterIsUpperLower(MY_STRING); + assertEquals(4, result.getUppercaseCount()); + assertEquals(31, result.getLowercaseCount()); + } + + @Test + void whenUsingCountByStreamApi_thenGetExpectedResult() { + LetterCount result = LetterCount.countByStreamAPI(MY_STRING); + assertEquals(4, result.getUppercaseCount()); + assertEquals(31, result.getLowercaseCount()); + } + + @Test + void whenUsingIsUpperCaseAndIsLowerCase_thenUnicodeCharactersCanBeChecked() { + assertTrue(Character.isLowerCase('ä')); + assertTrue(Character.isUpperCase('Ä')); + } +} + +class LetterCount { + private int uppercaseCount; + private int lowercaseCount; + + private LetterCount(int uppercaseCount, int lowercaseCount) { + this.uppercaseCount = uppercaseCount; + this.lowercaseCount = lowercaseCount; + } + + public static LetterCount countByCharacterRange(String input) { + int upperCount = 0; + int lowerCount = 0; + for (char c : input.toCharArray()) { + if (c >= 'A' && c <= 'Z') { + upperCount++; + } + if (c >= 'a' && c <= 'z') { + lowerCount++; + } + } + return new LetterCount(upperCount, lowerCount); + } + + public static LetterCount countByCharacterIsUpperLower(String input) { + int upperCount = 0; + int lowerCount = 0; + for (char c : input.toCharArray()) { + if (Character.isUpperCase(c)) { + upperCount++; + } + if (Character.isLowerCase(c)) { + lowerCount++; + } + } + return new LetterCount(upperCount, lowerCount); + } + + public static LetterCount countByStreamAPI(String input) { + return new LetterCount( + (int) input.chars().filter(Character::isUpperCase).count(), + (int) input.chars().filter(Character::isLowerCase).count() + ); + } + + public int getUppercaseCount() { + return uppercaseCount; + } + + public int getLowercaseCount() { + return lowercaseCount; + } +} \ No newline at end of file