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:
|
||||
### Relevant Articles:
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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