首页 > 试题广场 >

句子逆序

[编程题]句子逆序
  • 热度指数:501410 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”

所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符

数据范围:输入的字符串长度满足

注意本题有多组输入

输入描述:

输入一个英文语句,每个单词用空格隔开。保证输入只包含空格和字母。



输出描述:

得到逆序的句子

示例1

输入

I am a boy

输出

boy a am I
示例2

输入

nowcoder

输出

nowcoder
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    // 注意 hasNext 和 hasNextLine 的区别
    while (in.hasNextLine()) { // 注意 while 处理多个 case
        String a = in.nextLine();
        String[] arr = a.split(" ");
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.print(arr[i]);
            if (i > 0) {
                System.out.print(" ");
            }
        }
    }
}

发表于 2024-05-06 07:42:06 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] input = in.nextLine().split(" ");
        for(int i=input.length-1;i>=0;i--) {
            System.out.print(input[i]+" ");
        }
    }
}

发表于 2024-04-24 10:33:23 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();

        String[] split = s.split(" ");

        if(split.length == 1){
            System.out.println(s);
        } else {
            String str="";
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<split.length;i++){
                sb.append(split[split.length-1-i]).append(" ");
            }
            sb.deleteCharAt(sb.length()-1);
            System.out.println(sb.toString());

        }
    }
}
发表于 2024-04-18 16:43:50 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] s = str.split(" ");
        for (int i = s.length - 1; i >  0; i--) {
            System.out.print(s[i] + " ");
        }
        System.out.print(s[0]);
    }
}
编辑于 2024-04-15 19:19:52 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String[] arr = str.split(" ");

        StringBuilder builder = new StringBuilder();
        for (int i = arr.length - 1; i >= 0; i--) {
            builder.append(i == 0 ? arr[i] : arr[i] + " ");
        }
        System.out.println(builder.toString());
    }
}


编辑于 2024-03-06 19:31:23 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str =  in.nextLine();
        if(str.contains(" ")){
            String [] strArr = str.split(" ");
            for(int i = strArr.length - 1; i >= 0 ; i--){
                System.out.print(strArr[i] + " ");
            }
        }else{
            System.out.print(str);
        }
    }
}

编辑于 2024-01-28 22:45:18 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s = sc.nextLine();
        String[] split = s.split(" ");
        for (int i = split.length-1; i >=0 ; i--) {
            System.out.print(split[i]+" ");
        }
    }
}

发表于 2023-11-15 19:54:41 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (sc.hasNextLine()){
            String str = sc.nextLine();
            while(str.lastIndexOf(' ') > 0){  //第一个字符串之前不会有空格,所以循环在这之前就应该结束
                System.out.print(str.substring(str.lastIndexOf(' ') + 1,str.length()) + ' '); //截取最后一个空格后的字符串
                str = str.substring(0,str.lastIndexOf(' '));//将截取后的字符串删掉,准备下一次截取
            }
            System.out.print(str);//最后一次截取完成时,字符串str恰好只剩[0,第一个空格),所以直接输出即为第一个字符串
        }
    }
}
发表于 2023-10-11 15:03:02 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String []args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        for (int i = str.length() - 1; i >= 0; i--) {
            if (str.charAt(i) == ' ') {
                int j = 1;
                while (str.charAt(i + j) != ' ') {
                    if (i + j == str.length() - 1) {
                        System.out.print(str.charAt(i + j));
                        break;
                    } else {
                        System.out.print(str.charAt(i + j));
                        j++;
                    }
                }
                System.out.print(" ");
            }
            if (i == 0) {
                int j = 0;
                while (str.charAt(i + j) != ' ') {
                    if (i + j == str.length() - 1) {
                        System.out.print(str.charAt(i + j));
                        break;
                    } else {
                        System.out.print(str.charAt(i + j));
                        j++;
                    }
                }
            }
        }
    }
}
发表于 2023-09-13 16:23:00 回复(1)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] strArray = in.nextLine().split(" ");
        for (int i = strArray.length - 1; i >= 0; i--) {
            if (i == strArray.length - 1) {
                System.out.print(strArray[i]);
                continue;
            }
            System.out.print(" " + strArray[i]);
        }
    }
}


发表于 2023-08-08 22:10:10 回复(0)
为什么没人使用正则表达式处理空格的问题呢?
String[] words = str.split("(?<=\\s)|(?=\\s)");
这样不就直接解决了字符串以空格分割但是保留空格了么?


发表于 2023-08-05 20:29:39 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        String[] split = input.split(" ");
        for (int i = split.length - 1; i >= 0; i--) {
            System.out.print(split[i] + " ");
        }
    }
}

发表于 2023-08-05 09:16:34 回复(0)
public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] s1 = s.split(" ");
        for (int i = s1.length-1; i >=0; i--) {
            System.out.print(s1[i]+" ");
        }
    }
发表于 2023-07-01 16:19:15 回复(0)
import java.util.Scanner; 

import java.util.Stack;

// Stack栈 empty判空/push入栈/pop出栈/peek获取栈顶值/search查看是否在栈中
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner in = new Scanner(System.in);
        Stack<String> stack = new Stack<String>();
        while(in.hasNext()){
            stack.push(in.next());
        }
        while(!stack.empty()){
            System.out.print(stack.pop()+" ");
        }
        
    }
}

发表于 2023-06-21 00:04:54 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
           String s = in.nextLine();
           String[] strs = s.split(" ");
           for(int i = strs.length-1;i>=0;i--){
                System.out.print(strs[i]+" ");
           }
        }
    }
}
发表于 2023-05-24 14:55:03 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String line = in.nextLine();
        List<String> strings = Arrays.asList(line.split(" "));
        Collections.reverse(strings);
        for (String string : strings) {
            System.out.print(string + " ");
        }
    }
}
发表于 2023-05-14 22:04:13 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            StringBuilder sb = new StringBuilder();
            String newA = sb.append(a).reverse().toString();
            String [] aArr = newA.split(" ");
            sb.delete(0,a.length());
            for(int i = 0; i < aArr.length; i++){
                sb.append(aArr[i]).reverse().append(" ");
                System.out.print(sb.toString());
                if(aArr.length < aArr.length - 1) sb.delete(sb.length()-1,sb.length());
                sb.delete(0,sb.length());
            }        
        }
    }
}
发表于 2023-04-28 09:45:46 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String[] str2 = str.split(" ");
        for (int i = str2.length - 1; i >= 0; i--) {
            System.out.print(str2[i] + " ");
        }
    }
}
发表于 2023-04-06 18:01:03 回复(0)
import java.util.*;
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String msg = br.readLine();
        char[] ch = msg.toCharArray();
        msg = reverse(ch, 0, msg.length() - 1);
        int begin = 0;
        for (int i = 1; i < msg.length(); i++) {
            if (ch[i] == ' ') { //遇到空格
                reverse(ch, begin, i - 1); //以' '为分隔符的单词翻转
                begin = i + 1; // 新的单词开始下标
            }
        }
        //最后一个单词之后没有空格,也要记得翻转
        reverse(ch, begin, ch.length-1);
        System.out.println(new String(ch));
    }

    public static String reverse(char[] ch, int front, int end) {
        while (front < end) {
            ch[front] = (char) (ch[front] ^ ch[end]);
            ch[end] = (char) (ch[front] ^ ch[end]);
            ch[front] = (char) (ch[front] ^ ch[end]);
            front++;
            end--;
        }
        return new String(ch);
    }
}
发表于 2023-03-31 13:27:52 回复(0)