ふわ

2021年04月04日に参加

学習履歴詳細

【4回目】英語の文章と単語を逆にして出力する

String Builderクラス

・ミュータブルな文字のシーケンス
String Bufferクラスと互換のAPIを提供するが、synchronizationには対応していない。(複数のスレッドで文字列を共有する場合には対応していない??)
・主にappendメソッドやinsertメソッドを使用できる。
・内部のバッファが溢れた場合は自動で新しくメモリ領域を確保してくれる。

第3問

<問題>
Reversing letters and words: Write a program that reverses the letters of each word and a program that reverses the letters of each word and the words themselves.

<自分の解答>
```java
package S_3_myanswer;

public class Main {
    public static void main(String[] args) {
        // Given String
        String str = "This is a pen.";
        System.out.print(reversingLettersAndWords(str));
    }

    public static String reversingLettersAndWords(String str){
        StringBuffer sb = new StringBuffer();

        String[] strArray = str.split(" ");
        for (int i = strArray.length - 1; i >= 0; i--){
            StringBuffer word = new StringBuffer();
            for (int j = strArray[i].length() - 1; j >= 0; j--){
                word.append(strArray[i].charAt(j));
            }
            sb.append(word + " ");
        }
        return sb.toString();
    }
}
## 取り組んでいる問題集
Leonard, Anghel. Java Coding Problems: Improve your Java Programming skills by solving real-world coding challenges
Java

2023年06月11日(日)

1.1時間