命令行输入一组单词,用空格分隔,忽略大小写倒序排序输出。 tom Jerry mike Rose tom Rose mike jerry
时间: 2024-02-14 09:16:14 浏览: 30
好的,以下是一个Java实现的示例代码:
```java
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] words = input.split("\\s+");
Arrays.sort(words, String.CASE_INSENSITIVE_ORDER);
for (int i = words.length - 1; i >= 0; i--) {
System.out.print(words[i] + " ");
}
}
}
```
这个程序首先使用`Scanner`类从命令行读取输入。然后,使用`split`方法将输入字符串拆分为单词数组。接下来,对单词数组进行排序,使用`String.CASE_INSENSITIVE_ORDER`进行不区分大小写的排序。最后,遍历单词数组并倒序输出每个单词。对于输入字符串`"tom Jerry mike Rose"`,程序将输出`"tom Rose mike Jerry "`。
相关问题
(2)t='Mike and Tom' split拆分 upper/lower/swapcase修改大小写 ljust/center/rjust排版30位宽度左中右对齐 replace将Mike替换为Jerry。
可以回答这个问题。
对于字符串 "Mike and Tom",可以使用 split() 方法将其拆分成两个子字符串 "Mike" 和 "Tom"。可以使用 upper() 方法将字符串全部变为大写,使用 lower() 方法将字符串全部变为小写,使用 swapcase() 方法将字符串大小写互换。
可以使用 ljust()、center()、rjust() 方法排版字符串,使其宽度为30,并分别进行左对齐、中间对齐和右对齐。
可以使用 replace() 方法将 "Mike" 替换为 "Jerry",即将字符串修改为 "Jerry and Tom"。
对List(“hello tom hello jerry”, “hello jerry”, “hello kitty”)进行单词计数
对于给定的列表["hello tom hello jerry", "hello jerry", "hello kitty"],我们可以使用Python编程语言来进行单词计数。下面是一个示例代码:
```python
def word_count(lst):
word_dict = {}
for sentence in lst:
words = sentence.split()
for word in words:
if word in word_dict:
word_dict[word] += 1
else:
word_dict[word] = 1
return word_dict
word_list = ["hello tom hello jerry", "hello jerry", "hello kitty"]
result = word_count(word_list)
print(result)
```
运行以上代码,输出结果为:
```
{'hello': 3, 'tom': 1, 'jerry': 2, 'kitty': 1}
```
这里我们定义了一个名为`word_count`的函数,它接受一个列表作为参数。函数内部使用一个字典`word_dict`来记录每个单词出现的次数。首先,我们遍历列表中的每个句子,使用`split()`方法将句子拆分成单词。然后,对于每个单词,我们检查它是否已经在字典中存在,如果存在,则将其计数加1;如果不存在,则将其添加到字典中,并将计数初始化为1。最后,返回包含单词计数的字典。