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();
        }
    
    }
  • 相关阅读:
    HTML-代码定义
    数组
    for。。。for嵌套if,if嵌套for。
    输入年月日, 判断输入的是否正确
    日期功能
    方程
    5.8 一维数组
    5.9 二维数组
    5.7 类
    5.4穷举,迭代
  • 原文地址:https://www.cnblogs.com/watchfree/p/5308159.html
Copyright © 2011-2022 走看看