問題 F: C語言考試練習題_字符逆序

 

問題 F: C語言考試練習題_字符逆序

時間限制: 1 Sec  內存限制: 64 MB
提交: 996  解決: 781
[提交] [狀態] [命題人:外部導入]

題目描述

將一個字符串str的內容顛倒過來,並輸出。str的長度不超過100個字符。

輸入

輸入包括一行。
第一行輸入的字符串。

輸出

輸出轉換好的逆序字符串。

樣例輸入 Copy

I am a student

樣例輸出 Copy

tneduts a ma I

提示

信息學院07&08學年第二學期C語言上機考試

解法1:StirngBuffer

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        Scanner scan = new Scanner(System.in);
        //int n = scan.nextInt();
        String temp = scan.nextLine();
        //String later = scan.next();
        //int n = scan.nextInt();
        StringBuffer sb = new StringBuffer();
        sb.append(temp);
        System.out.println(sb.reverse());
    }

}
 

解法2:遍歷逆轉

import java.util.Scanner;


public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        for(int i=str.length()-1;i>=0;i--){
            System.out.printf("%c",str.charAt(i));
        }
        System.out.println();
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章