114-2 OOP B組 Week08 實習課練習 Problem2


Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
Java 19, Java 8

題目說明

使用者輸入一個字串s,請撰寫一個遞迴副程反轉s並回傳輸出。


題目要求

  1. 必須使用「遞迴」實作(不可使用迴圈)。
  2. 不可使用內建反轉函式(例如 StringBuilder.reverse())。
  3. 需正確處理:
    • 空字串 ""
    • 單一字元

參考下方程式碼作答:

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        sc.close();
        String reversedS = getReversedS(s);
        System.out.println(reversedS);
    }

    static String getReversedS(String s) {

    }
}

測資範圍

  • s可能是空字串。
  • \[0 <= s.length() <= 100\]

範例輸入/輸出

範例輸入1

hello world

範例輸出1

dlrow olleh

範例輸入2

範例輸出2


範例輸入3

f

範例輸出3

f

Comments

There are no comments at the moment.