/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/strings/Upper.java
39 строк
1 KB
p@ren
Refactor and enhance the 'Upper' class (#6118)
12 янв 2025, 14:29
Не верифицирован
12 янв 2025, 14:29
bd785de
Код
Авторство
О чём код?
package com.thealgorithms.strings; public final class Upper { private Upper() { } /** * Driver Code */ public static void main(String[] args) { String[] strings = {"ABC", "ABC123", "abcABC", "abc123ABC"}; for (String s : strings) { assert toUpperCase(s).equals(s.toUpperCase()); } } /** * Converts all the characters in this {@code String} to upper case * * @param s the string to convert * @return the {@code String}, converted to uppercase. */ public static String toUpperCase(String s) { if (s == null) { throw new IllegalArgumentException("Input string connot be null"); } if (s.isEmpty()) { return s; } StringBuilder result = new StringBuilder(s); for (int i = 0; i < result.length(); ++i) { char currentChar = result.charAt(i); if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) { result.setCharAt(i, Character.toUpperCase(currentChar)); } } return result.toString(); } }