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();
        }
    
    }
  • 相关阅读:
    【git hub使用】
    【struct2 第一天】
    【JSP基础 第一天】
    【Java基础学习 day01】
    网站建设 【Django】 【MTV】
    Python-Json字符串和XML解析
    Python-冒泡和快排
    Python-面向对象编程
    练习-字符串编码
    练习-统计文件中单词数量
  • 原文地址:https://www.cnblogs.com/watchfree/p/5308159.html
Copyright © 2011-2022 走看看