[count-upper-lower] count upper/lowercase letters

This commit is contained in:
Kai.Yuan 2024-01-31 23:27:16 +01:00
parent 3a2f05b17d
commit 6bbef27bc7
3 changed files with 121 additions and 74 deletions

View File

@ -1,2 +1,2 @@
### Relevant Articles:
### Relevant Articles:
>>>>>>> be317465c ([count-upper-lower] count upper/lowercase letters)

View File

@ -1,72 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-8</artifactId>
<name>core-java-string-operations-8</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<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>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<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>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-8</artifactId>
<name>core-java-string-operations-8</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,85 @@
package com.baeldung.countupperandlowercasechars;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CountUpperAndLowercaseCharsUnitTest {
private static final String MY_STRING = "Hi, Welcome to Baeldung! Let's count letters!";
private static final LetterCount EXPECTED = new LetterCount(4, 31);
@Test
void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() {
int upperCnt = 0;
int lowerCnt = 0;
for (char c : MY_STRING.toCharArray()) {
if (c >= 'A' && c <= 'Z') {
upperCnt++;
}
if (c >= 'a' && c <= 'z') {
lowerCnt++;
}
}
LetterCount result = new LetterCount(upperCnt, lowerCnt);
assertEquals(EXPECTED, result);
}
@Test
void whenUsingCharacterIsLowerOrUpperCase_thenGetExpectedResult() {
int upperCnt = 0;
int lowerCnt = 0;
for (char c : MY_STRING.toCharArray()) {
if (Character.isUpperCase(c)) {
upperCnt++;
}
if (Character.isLowerCase(c)) {
lowerCnt++;
}
}
LetterCount result = new LetterCount(upperCnt, lowerCnt);
assertEquals(EXPECTED, result);
}
@Test
void whenUsingStreamFilterAndCount_thenGetExpectedResult() {
LetterCount result = new LetterCount(
(int) MY_STRING.chars().filter(Character::isUpperCase).count(),
(int) MY_STRING.chars().filter(Character::isLowerCase).count()
);
assertEquals(EXPECTED, result);
}
}
class LetterCount {
private int uppercaseCnt;
private int lowercaseCnt;
public LetterCount(int uppercaseCnt, int lowercaseCnt) {
this.uppercaseCnt = uppercaseCnt;
this.lowercaseCnt = lowercaseCnt;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof LetterCount)) {
return false;
}
LetterCount that = (LetterCount) o;
if (uppercaseCnt != that.uppercaseCnt) {
return false;
}
return lowercaseCnt == that.lowercaseCnt;
}
@Override
public int hashCode() {
int result = uppercaseCnt;
result = 31 * result + lowercaseCnt;
return result;
}
}