実践:文字列処理

このレッスンはPhase 2のハンズオン実践で、5つのプロジェクトを通じて文字列処理スキルを定着させます。

プロジェクト1:文字列反転

使用スキル:StringBuilder、String.toCharArray()。

要件

文字列を反転するメソッドを作成します。

実装

JAVA
public class StringReverse {
    // 方法1:StringBuilder
    public static String reverse1(String str) {
        if (str == null) return null;
        return new StringBuilder(str).reverse().toString();
    }
    
    // 方法2:文字配列
    public static String reverse2(String str) {
        if (str == null) return null;
        char[] chars = str.toCharArray();
        int left = 0, right = chars.length - 1;
        while (left < right) {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }
        return new String(chars);
    }
    
    // 方法3:再帰
    public static String reverse3(String str) {
        if (str == null || str.length() <= 1) {
            return str;
        }
        return reverse3(str.substring(1)) + str.charAt(0);
    }
    
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println("元の文字列: " + s);
        System.out.println("反転1: " + reverse1(s));
        System.out.println("反転2: " + reverse2(s));
        System.out.println("反転3: " + reverse3(s));
    }
}

出力:

TEXT
元の文字列: Hello, World!
反転1: !dlroW ,olleH
反転2: !dlroW ,olleH
反転3: !dlroW ,olleH

プロジェクト2:単語数カウント

使用スキル:String.split()、for-eachループ。

要件

文字列内の単語数を数えます。

実装

JAVA
public class WordCount {
    public static int countWords(String str) {
        if (str == null || str.trim().isEmpty()) {
            return 0;
        }
        // 空白で分割、trimで先頭・末尾の空白を削除
        String[] words = str.trim().split("\\s+");
        return words.length;
    }
    
    public static void main(String[] args) {
        System.out.println(countWords("Hello World"));           // 2
        System.out.println(countWords("  Hello   World  "));     // 2
        System.out.println(countWords("Java is awesome"));       // 3
        System.out.println(countWords(""));                      // 0
        System.out.println(countWords(null));                    // 0
    }
}

プロジェクト3:回文検出

使用スキル:StringBuilder、二重ポインタ。

要件

文字列が回文かどうかを判定します(前から読んでも後ろから読んでも同じ)。

実装

JAVA
public class Palindrome {
    // 方法1:StringBuilderで反転
    public static boolean isPalindrome1(String str) {
        if (str == null) return false;
        String reversed = new StringBuilder(str).reverse().toString();
        return str.equals(reversed);
    }
    
    // 方法2:二重ポインタ
    public static boolean isPalindrome2(String str) {
        if (str == null) return false;
        int left = 0, right = str.length() - 1;
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
    
    // 大文字小文字と英数字以外を無視
    public static boolean isPalindromeIgnore(String str) {
        if (str == null) return false;
        String cleaned = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        return isPalindrome2(cleaned);
    }
    
    public static void main(String[] args) {
        System.out.println(isPalindrome1("racecar"));        // true
        System.out.println(isPalindrome1("hello"));          // false
        System.out.println(isPalindromeIgnore("A man, a plan, a canal: Panama"));  // true
    }
}

プロジェクト4:日付計算

使用スキル:LocalDate、ChronoUnit、Period。

要件

2つの日付間の日数、年月日差を計算します。

実装

JAVA
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class DateCalculator {
    // 日数を計算
    public static long daysBetween(LocalDate start, LocalDate end) {
        return ChronoUnit.DAYS.between(start, end);
    }
    
    // 年月日差を計算
    public static Period periodBetween(LocalDate start, LocalDate end) {
        return Period.between(start, end);
    }
    
    // うるう年を判定
    public static boolean isLeapYear(int year) {
        return LocalDate.of(year, 1, 1).isLeapYear();
    }
    
    // 月の日数を取得
    public static int daysInMonth(int year, int month) {
        return LocalDate.of(year, month, 1).lengthOfMonth();
    }
    
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2026, 1, 1);
        LocalDate end = LocalDate.of(2026, 12, 31);
        
        System.out.println("日数: " + daysBetween(start, end));  // 364
        
        Period period = periodBetween(start, end);
        System.out.println("期間: " + period);  // P11M30D
        
        System.out.println("2026年はうるう年: " + isLeapYear(2026));  // false
        System.out.println("2024年はうるう年: " + isLeapYear(2024));  // true
        System.out.println("2026年2月の日数: " + daysInMonth(2026, 2));  // 28
    }
}

プロジェクト5:文字列暗号化

使用スキル:charAt()、StringBuilder、ASCII算術。

要件

簡単なシーザー暗号を実装します(各文字を3つずらす)。

実装

JAVA
public class CaesarCipher {
    public static String encrypt(String text, int shift) {
        StringBuilder result = new StringBuilder();
        
        for (char c : text.toCharArray()) {
            if (Character.isLetter(c)) {
                char base = Character.isUpperCase(c) ? 'A' : 'a';
                // 暗号化: (文字 - 基準 + シフト) % 26 + 基準
                char encrypted = (char) ((c - base + shift) % 26 + base);
                result.append(encrypted);
            } else {
                result.append(c);
            }
        }
        
        return result.toString();
    }
    
    public static String decrypt(String text, int shift) {
        return encrypt(text, 26 - shift);
    }
    
    public static void main(String[] args) {
        String original = "Hello, World!";
        int shift = 3;
        
        String encrypted = encrypt(original, shift);
        String decrypted = decrypt(encrypted, shift);
        
        System.out.println("元のテキスト: " + original);
        System.out.println("暗号化: " + encrypted);    // Khoor, Zruog!
        System.out.println("復号化: " + decrypted);    // Hello, World!
    }
}

追加演習

演習1:文字頻度

JAVA
import java.util.HashMap;
import java.util.Map;

public class CharFrequency {
    public static Map<Character, Integer> frequency(String str) {
        Map<Character, Integer> map = new HashMap<>();
        for (char c : str.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        return map;
    }
    
    public static void main(String[] args) {
        String s = "hello world";
        Map<Character, Integer> freq = frequency(s);
        
        for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
            System.out.println("'" + entry.getKey() + "' : " + entry.getValue());
        }
    }
}

演習2:キャメルケース変換

JAVA
public class CamelCase {
    // アンダースコアからキャメルケースへ
    public static String toCamelCase(String str) {
        StringBuilder result = new StringBuilder();
        boolean capitalizeNext = false;
        
        for (char c : str.toCharArray()) {
            if (c == '_') {
                capitalizeNext = true;
            } else {
                if (capitalizeNext) {
                    result.append(Character.toUpperCase(c));
                    capitalizeNext = false;
                } else {
                    result.append(c);
                }
            }
        }
        
        return result.toString();
    }
    
    // キャメルケースからアンダースコアへ
    public static String toSnakeCase(String str) {
        StringBuilder result = new StringBuilder();
        
        for (char c : str.toCharArray()) {
            if (Character.isUpperCase(c)) {
                result.append('_');
                result.append(Character.toLowerCase(c));
            } else {
                result.append(c);
            }
        }
        
        return result.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(toCamelCase("hello_world"));  // helloWorld
        System.out.println(toSnakeCase("helloWorld"));   // hello_world
    }
}

❓ よくある質問

Q 文字列操作が遅い場合はどうすればいいですか?
A 一括連結にはStringBuilderを使用し、頻繁な検索には正規表現を使用してください。
Q nullと空の文字列をどう処理すればいいですか?
A メソッドの先頭でnullと空をチェックし、合理的なデフォルト値を返すか、例外をスローしてください。
Q 正規表現をどう学べばいいですか?
A 基本(. * + ? [] ())から始めて、徐々に高度な使い方を学んでください。

📖 まとめ

📝 演習

  1. 文字列圧縮: "aaabbbcc"を"a3b3c2"に圧縮
  2. メール検証: 文字列が有効なメール形式かどうかを判定
  3. 日付ユーティリティクラス: 年齢計算、日数差、フォーマットなどのメソッドを持つDateUtilsクラスを作成

次のレッスン

次のレッスンでは、Phase 3に移り、クラスとオブジェクトを学びます — オブジェクト指向プログラミングの世界に入ります。

100%