反转“Hello World”的每个单词Java 中的字符串
我想反转 Java 中字符串的每个单个单词(不是整个字符串,只是每个单词)。
示例:如果输入字符串是“Hello World”,则输出应为“olleH dlroW”。
I want to reverse each individual word of a String in Java (not the entire string, just each individual word).
Example: if input String is "Hello World" then the output should be "olleH dlroW".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
简单的方法:
Easy way:
这会反转给定字符串中的单词。假定单词之间由一个空格分隔。反转是就地完成的(在字符缓冲区中)。
This reverses the words in the given string. Words are assumed to be separated by a single space. Reversal is done in place (in the character buffer).
跑步:
Run:
流 API 示例
example with stream API
您可以使用增强型 for 循环反转字符串并交换其中的加数:
输出:
You can reverse a string using the enhanced for loop and swap the summands inside:
Output:
逐块反向复制字符串,然后连接空格。
例如。 “你好java世界”。
第一个块=“hello”反向复制它:-“olleh”然后添加空格
第二块=“java”等。
程序也适用于单词之间的多个空格。
Reverse copy the string block-wise and then concatenate the whitespaces.
for eg. "hello java world".
1st block = "hello" reverse copy it:- "olleh" and add whitespace then
2nd block = "java" etc.
Program also works for multiple whitespaces between words.
上述一些解决方案的运行时复杂度较高。使用下面的算法,可以在 O(n) 时间内实现。
算法:
复杂度:O(n),其中 n 是字符串的长度。
Some of the above solutions are of the higher run time complexities. With the below algorithm, it can be achieved in O(n) time.
Algorithm:
Complexity: O(n) where n is the length of the String.
TC - O(n) 和 SC - O(1) 的解决方案
Solution with TC - O(n) and SC - O(1)
使用 split() 函数并反转单个单词
使用 trim() 删除在新字符串末尾添加的额外空格
输出:
Use split() function and reverse individual words
Remove the extra space that is added at the end of the new String using trim()
Output:
使用 split(),您只需更改您想要拆分的内容。
Using split(), you just have to change what you wish to split on.
我在解决这个问题时想出了这个答案。我尝试不使用嵌套 for 循环解决方案 O(N^2)。我强迫自己使用堆栈来娱乐:D
I came up with this answer while working on the problem. I tried not to use nested for loop solution O(N^2). I kind of forced myself to use stack for fun :D
跑步:
run:
考虑到分隔符可以是多个空格/制表符,并且我们希望保留它们:
Taking into account that the separator can be more than one space/tab and that we want to preserve them:
这是一个接受字符串并将其反转的方法。
首先,您需要将字符串拆分为这样的单词
Heres a method that takes a string and reverses it.
First you need to split the string into words like this
我假设您可以只打印结果(您刚才说“输出应该是...”);-)
或者返回反转的字符串:
I'm assuming you could just print the results (you just said 'the output should be...') ;-)
Or returning the reversed String:
仅使用
substring()
和递归:Using only
substring()
and recursion:好吧,我是一名 C/C++ 人员,在面试中练习 java 让我知道是否可以更改或改进某些内容。以下允许多个空格和换行符。
第一个是使用 StringBuilder
这个是使用 char[]。我觉得这样更有效率...
Well I'm a C/C++ guy, practicing java for interviews let me know if something can be changed or bettered. The following allows for multiple spaces and newlines.
First one is using StringBuilder
This one is using char[]. I think its more efficient...
这里没有人考虑 unicode 字符。您需要使用 java.text.BreakIterator 来查找单词边界,然后在每个单词边界内使用另一个边界来枚举字符边界:
使用上面的简单方法将移动变音符号
\u0308String
时,code> 位于第一个l
之上。您希望它保持在e
之上。No one here is considering unicode characters. You need to use
java.text.BreakIterator
to find word boundaries and then use another one within each word boundary to enumerate character boundaries:Using naive methods above will shift the diacritic character
\u0308
above the firstl
when you reverse theString
. You want it to stay above thee
.这是最简单的解决方案,甚至不使用任何循环。
即使这是作业,也可以随意复制并作为您自己的作业提交。你要么获得额外的学分(如果你能解释它是如何工作的),要么因抄袭而被抓(如果你不能)。
Here's the simplest solution that doesn't even use any loops.
Even if this is homework, feel free to copy it and submit it as your own. You'll either get an extra credit (if you can explain how it works) or get caught for plagiarism (if you can't).
在
分割
为单词数组
后,您需要对每个单词执行此操作。You need to do this on each word after you
split
into anarray
of words.了解您的图书馆;-)
Know your libraries ;-)
这应该可以解决问题。这将迭代源字符串中的每个单词,并使用
StringBuilder
内置的reverse()
方法,并输出反转的单词。输出:
注释:评论者正确地指出了一些我认为应该在这里提到的事情。此示例将在结果末尾附加一个额外的空格。它还假设您的单词之间用一个空格分隔,并且您的句子不包含标点符号。
This should do the trick. This will iterate through each word in the source string, reverse it using
StringBuilder
's built-inreverse()
method, and output the reversed word.Output:
Notes: Commenters have correctly pointed out a few things that I thought I should mention here. This example will append an extra space to the end of the result. It also assumes your words are separated by a single space each and your sentence contains no punctuation.