一串字符代表排列好之后的舞蹈演员的演出服颜色,颜色用一串英文单词表示,两个舞蹈演员之间用空格隔开。(演员人数不超过20,单词长度不超过10)
时间: 2024-05-27 10:14:01 浏览: 6
A string represents the colors of the dance performers' costumes after being arranged. The colors are represented by a string of English words, and the space is used to separate two dance performers. (The number of performers does not exceed 20, and the length of each word does not exceed 10.)
相关问题
用C语言求一个字符串中有多少个单词,单词之间用空格分隔,但两个单词之间可能有多个空格
可以使用以下代码来实现:
```c
#include <stdio.h>
#include <ctype.h>
int count_words(char *str) {
int count = 0, is_word = 0;
while (*str) {
if (isspace(*str)) {
is_word = 0;
} else if (!is_word) {
is_word = 1;
count++;
}
str++;
}
return count;
}
int main() {
char str[] = " This is a test string. ";
int count = count_words(str);
printf("The number of words in the string is: %d\n", count);
return 0;
}
```
在 `count_words` 函数中,使用一个 `is_word` 变量来记录当前是否在一个单词中。遍历字符串,如果遇到空格,则将 `is_word` 设为 0;如果遇到一个非空格字符并且当前不在一个单词中,则将 `is_word` 设为 1,并将单词计数器加 1。最后返回计数器的值即可。
java输入一个字符串(单词之间用空格隔开),输出不重复的单词
可以使用Java中的Set集合来实现去重。具体实现步骤如下:
1. 从控制台读取输入的字符串;
2. 使用字符串的 `split()` 方法将字符串按照空格拆分成字符串数组;
3. 创建一个空的 `HashSet` 集合用于存储不重复的单词;
4. 遍历字符串数组,将每个单词添加到 HashSet 集合中;
5. 遍历 HashSet 集合,输出不重复的单词。
以下是示例代码:
```java
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] words = str.split(" ");
Set<String> set = new HashSet<>();
for (String word : words) {
set.add(word);
}
for (String word : set) {
System.out.println(word);
}
}
}
```
输入示例:
```
hello world hello java world
```
输出结果:
```
hello
world
java
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)