杭電acm1062 Text Reverse

Text Reverse

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34449 Accepted Submission(s): 13532

Problem Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
?
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
?
Output
For each test case, you should output the text which is processed.
?
Sample Input
3 olleh !dlrow m'I morf .udh I ekil .mca
?
Sample Output
hello world! I'm from hdu. I like acm.
Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.

Solution

//簡(jiǎn)單的字符串處理

Code

/**
 * date:2017.11.12
 * author:孟小德
 * function:acm試題1062
 *  Text Reverse 文本倒置
 */
 
 
 
import java.util.*;
 
public class acm1062
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
 
        int num = input.nextInt();
        input.nextLine();
 
        String[] string = new String[num];
        for (int i=0;i<num;i++)
        {
            string[i] = input.nextLine();
        }
 
        String[] result = new String[num];
        for (int i=0;i<num;i++)
        {
 
            String[] str = string[i].split(" ");    //分割字符串
 
            result[i] = stringReverse(str[0]);
            for (int j = 1;j<str.length;j++)
            {
                result[i] = result[i] + " " + stringReverse(str[j]);
            }
 
            //末尾空格加上
            int n = string[i].length() - result[i].length();
            for (int j = 0;j<n;j++)
            {
                result[i] += " ";
            }
 
        }
 
        for (int i = 0;i<num;i++)
        {
            System.out.println(result[i]);
        }
    }
 
    //翻轉(zhuǎn)單詞字母
    public static String stringReverse(String str)
    {
 
        if (str.equals(""))
        {
            return "";
        }
        else
        {
            String string = String.valueOf(str.charAt(str.length()-1));
            for (int i=str.length()-2;i>=0;i--)
            {
                string += String.valueOf(str.charAt(i));
 
            }
 
            return string;
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今晚突然想起我高三的室友,想到她是因?yàn)榻裉煸缟衔以诖采系囊幌盗行睦锒窢?zhēng)。當(dāng)初高三的時(shí)候,其實(shí)室友c基本算是我的競(jìng)爭(zhēng)...
    佛系肉團(tuán)閱讀 280評(píng)論 0 0
  • 微薰的目光 飛翔,只為尋找遠(yuǎn)方 向著心的方向 彼時(shí)的梔子花 淡開(kāi)了一季的憂傷 那細(xì)細(xì)小小的花影 在心田開(kāi)放 前路茫...
    葉淺予閱讀 330評(píng)論 1 0
  • 五月初五,端午節(jié)。遙想前年的端午節(jié),在烈日下?lián)]舞著槳,努力訓(xùn)練著,好像可以獲得龍舟賽冠軍一般。今年的端午,一直在路...
    中藥sama閱讀 323評(píng)論 0 0

友情鏈接更多精彩內(nèi)容