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"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>core-java-string-operations-8</artifactId>
|
<artifactId>core-java-string-operations-8</artifactId>
|
||||||
<name>core-java-string-operations-8</name>
|
<name>core-java-string-operations-8</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung.core-java-modules</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>core-java-modules</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<build>
|
||||||
<dependency>
|
<plugins>
|
||||||
<groupId>org.apache.commons</groupId>
|
<plugin>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<version>${apache.commons.lang3.version}</version>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
</dependency>
|
<configuration>
|
||||||
<dependency>
|
<source>${maven.compiler.source}</source>
|
||||||
<groupId>org.apache.commons</groupId>
|
<target>${maven.compiler.target}</target>
|
||||||
<artifactId>commons-text</artifactId>
|
</configuration>
|
||||||
<version>${commons-text.version}</version>
|
</plugin>
|
||||||
</dependency>
|
</plugins>
|
||||||
<dependency>
|
</build>
|
||||||
<groupId>org.liquibase</groupId>
|
|
||||||
<artifactId>liquibase-core</artifactId>
|
<properties>
|
||||||
<version>4.9.1</version>
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
<scope>test</scope>
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
</dependency>
|
</properties>
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
</project>
|
||||||
<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>
|
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user