zoukankan      html  css  js  c++  java
  • 1.2.2 Text_Reverse

    Text Reverse 

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)  

    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 ' ' after the interger T, then you may use gets() to read a line and process it.

       

    import java.util.Scanner;
    public class Main {
        static String reverseStr(String str){
            StringBuffer buf=new StringBuffer();
            buf.append(str);
            return buf.reverse().toString();
            
        }
        public static void main(String [] args){
            Scanner sc=new Scanner(System.in);
            int t=sc.nextInt();
            sc.nextLine();
            while(t-->0){
                String str1=sc.nextLine();
                String str[]=str1.split(" ");
                for(int i=0;i<str.length;i++){
                    System.out.print(reverseStr(str[i]));
                    if(i<str.length-1)
                        System.out.print(" ");
                }
                if(str1.endsWith(" "))System.out.print(" ");  ////尤其注意这里,表示最后一个字符后可能有空格  
                System.out.println();
            
            }
            sc.close();
        }
    
    }
  • 相关阅读:
    架构原则
    基于DDD的Lean Framework
    Javascript 内核Bug
    Back
    Exercise:函数应用于排序
    Lesson5:函数简单应用(二)
    lesson4: 函数简单应用
    lesson3: While 语句简单应用
    range 和len的并用
    lesson2: Python:for语句简单应用
  • 原文地址:https://www.cnblogs.com/watchfree/p/5308159.html
Copyright © 2011-2022 走看看