java的一道题:.给定一个字符串,逐个翻转字符串中的每个单词。(20分 )要求:输入字符串包括前导或者尾部空格时,反转后不能包括,如果两个单词间有多个空格,在反转字串时,两个词之间的空格减少到一个。 例如:输入:" Mozi is a technology company",输出 :“company technology a is Mozi"
时间: 2024-02-11 08:03:53 浏览: 132
这道题可以使用字符串分割和反转的方法实现。具体步骤如下:
1. 去除字符串开头和结尾的空格,可以使用`trim()`方法;
2. 使用空格将字符串分割成单词数组,可以使用`split()`方法;
3. 将单词数组反转,可以使用`Collections.reverse()`方法;
4. 将反转后的单词数组拼接成一个字符串,单词之间只有一个空格,可以使用`String.join()`方法。
示例代码如下:
```java
public static String reverseWords(String s) {
// 去除字符串开头和结尾的空格
s = s.trim();
// 使用空格将字符串分割成单词数组
String[] words = s.split("\\s+");
// 反转单词数组
List<String> list = Arrays.asList(words);
Collections.reverse(list);
// 将反转后的单词数组拼接成一个字符串
return String.join(" ", list);
}
```
使用样例:
```java
String s = " Mozi is a technology company ";
String result = reverseWords(s);
System.out.println(result);
```
输出结果:
```
company technology a is Mozi
```
相关问题
给定一个字符串将字符串的每个单词逐个翻转
将给定字符串中的每个单词逐个翻转,通常需要以下步骤:
1. 首先,确定字符串中单词的边界。单词通常由空格分隔,所以我们可以通过查找空格来确定单词的边界。
2. 然后,对于字符串中的每个单词,我们将其提取出来,并对提取出来的单词进行翻转操作。
3. 最后,将翻转后的单词重新组合回原来的字符串中。
举个例子,如果有一个字符串 "Hello World",我们需要先找到空格将字符串分割成 "Hello" 和 "World",然后翻转这两个单词得到 "olleH" 和 "dlroW",最后将它们拼接起来形成新的字符串 "olleH dlroW"。
在Python中,这个过程可以通过以下代码实现:
```python
def reverse_words(s):
# 分割字符串为单词列表
words = s.split(' ')
# 翻转每个单词并存储到新列表
reversed_words = [word[::-1] for word in words]
# 将翻转后的单词列表拼接成字符串
return ' '.join(reversed_words)
# 示例
input_string = "Hello World"
output_string = reverse_words(input_string)
print(output_string) # 输出: "olleH dlroW"
```
给定一个字符串,逐个翻转字符串中的每个单词。 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输入: " hello world! " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。 示例 3: 输入: "a good example" 输出: "example good a" 解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
可以使用 Java 的字符串操作和正则表达式来逐个翻转字符串中的每个单词。以下是一个实现的示例代码:
```java
public class WordReverse {
public static void main(String[] args) {
String str1 = "the sky is blue";
String str2 = " hello world! ";
String str3 = "a good example";
String reversedStr1 = reverseWords(str1);
String reversedStr2 = reverseWords(str2);
String reversedStr3 = reverseWords(str3);
System.out.println(reversedStr1); // 输出:blue is sky the
System.out.println(reversedStr2); // 输出:world! hello
System.out.println(reversedStr3); // 输出:example good a
}
public static String reverseWords(String s) {
// 去除首尾多余的空格,并将字符串按照空格分割成单词数组
String[] words = s.trim().split("\\s+");
StringBuilder sb = new StringBuilder();
// 从后往前遍历单词数组,将每个单词添加到结果字符串中
for (int i = words.length - 1; i >= 0; i--) {
sb.append(words[i]);
if (i > 0) {
sb.append(" ");
}
}
return sb.toString();
}
}
```
在上面的示例代码中,`reverseWords()` 方法接受一个字符串参数 `s`,首先使用 `trim()` 方法去除字符串两端的空格,然后使用 `split("\\s+")` 方法按照一个或多个空格将字符串分割成单词数组。
接下来,使用 `StringBuilder` 创建一个可变字符串 `sb`,从后往前遍历单词数组,将每个单词添加到 `sb` 中,并在单词之间添加一个空格。
最后,使用 `toString()` 方法将 `sb` 转换为字符串并返回。
运行上面的示例代码,可以得到预期的输出结果。
希望这个示例代码能帮助你解决问题!如果还有其他问题,请随时提问。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)