Merge pull request #15785 from sk1418/count-upper-lower
[count-upper-lower] count upper/lowercase letters
This commit is contained in:
commit
c8ac9ca720
|
@ -1,2 +1 @@
|
|||
|
||||
### Relevant Articles:
|
||||
|
|
|
@ -13,42 +13,6 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${apache.commons.lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>${commons-text.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
<version>4.9.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
<version>4.9.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -65,8 +29,6 @@
|
|||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
|
||||
<commons-text.version>1.10.0</commons-text.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue