Reverse a given sentence in Java

前端 未结 14 1941
别跟我提以往
别跟我提以往 2020-11-27 04:52

Can anyone tell me how to write a Java program to reverse a given sentence?

For example, if the input is:

\"This is an interview question\"

14条回答
  •  盖世英雄少女心
    2020-11-27 05:26

    Bozho already gave a great Java-specific answer, but in the event you ever need to solve this problem without Java API methods:

    To reverse, you can simply pop individual words onto a stack and pop them all back off when there are no words left.

    (Just to be extra clear, Java does provide a Stack class, so it is possible to use this method in Java as well).

提交回复
热议问题