不用指针,不用函数循环输入字符串【存在空格】,对每个单词实现逆置
时间: 2023-09-08 19:05:01 浏览: 67
### 回答1:
可以使用数组和循环来实现对每个单词的逆置,具体步骤如下:
1. 定义一个字符数组,用于存储输入的字符串。
2. 使用循环逐个读入字符,直到读入回车符为止。
3. 在循环中,判断当前字符是否为空格,如果是,则将前面的单词进行逆置。
4. 逆置单词的方法是使用双指针,一个指向单词的开头,一个指向单词的结尾,交换两个指针所指向的字符,直到两个指针相遇。
5. 重复步骤3和4,直到读入的字符为回车符为止。
示例代码如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入字符串:");
fgets(str, 100, stdin); // 读入字符串,包括空格和回车符
int len = strlen(str);
int start = , end = ;
for (int i = ; i < len; i++) {
if (str[i] == ' ' || str[i] == '\n') { // 遇到空格或回车符,逆置单词
end = i - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
start = i + 1;
}
}
printf("逆置后的字符串为:%s", str);
return ;
}
```
### 回答2:
方法一:
解题思路:使用字符串数组存储每个单词,然后依次逆置每个单词。
步骤如下:
1. 定义一个字符数组str[300]用于存储输入的字符串。
2. 使用scanf("%[^\n]%*c", str)获取输入的字符串。
3. 定义一个字符串数组word[100]用于存储每个单词。
4. 定义两个变量wordCnt和startIdx,分别表示单词数量和当前单词的起始位置,初始值均为0。
5. 使用循环遍历输入的字符串,当遇到空格或字符串结束符时,将截取的单词存入word数组中,并将wordCnt加1。
6. 使用循环遍历word数组中的每个单词,对每个单词进行逆置。
7. 使用循环输出逆置后的每个单词,单词之间用空格分隔。
代码示例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[300];
scanf("%[^\n]%*c", str);
char word[100][300];
int wordCnt = 0;
int startIdx = 0;
for (int i = 0; i <= strlen(str); i++) {
if (str[i] == ' ' || str[i] == '\0') {
strncpy(word[wordCnt], &str[startIdx], i - startIdx);
word[wordCnt][i - startIdx] = '\0';
startIdx = i + 1;
wordCnt++;
}
}
for (int i = 0; i < wordCnt; i++) {
int len = strlen(word[i]);
for (int j = 0; j < len / 2; j++) {
char temp = word[i][j];
word[i][j] = word[i][len - j - 1];
word[i][len - j - 1] = temp;
}
}
for (int i = 0; i < wordCnt; i++) {
printf("%s ", word[i]);
}
return 0;
}
```
方法二:
解题思路:使用两个指针分别指向单词的起始位置和结束位置,然后逆置两个指针之间的字符。
步骤如下:
1. 定义一个字符数组str[300]用于存储输入的字符串。
2. 使用scanf("%[^\n]%*c", str)获取输入的字符串。
3. 定义两个指针start和end,初始都指向字符串的起始位置。
4. 使用while循环,当end指向非空格字符时,将end向后移动一位。
5. 使用while循环,当start小于end时,交换start和end指向的字符,并分别向前和向后移动一位。
6. 重复步骤4和5,直到start或end指向字符串的结束符为止。
7. 使用循环遍历字符串,输出逆置后的结果。
代码示例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[300];
scanf("%[^\n]%*c", str);
char *start = str;
char *end = str;
while (*end) {
while (*end != ' ' && *end != '\0') {
end++;
}
char *tempStart = start;
char *tempEnd = end - 1;
while (tempStart < tempEnd) {
char temp = *tempStart;
*tempStart = *tempEnd;
*tempEnd = temp;
tempStart++;
tempEnd--;
}
start = ++end;
}
printf("%s", str);
return 0;
}
```
以上两种方法都可以实现对输入字符串中每个单词的逆置,你可以根据自己的需要选择其中一种方法来实现。
### 回答3:
要实现不用指针、不用函数循环输入字符串,并对每个单词进行逆置,可以使用以下方法:
1. 首先,创建一个空字符串变量用于存储输入的字符串。
2. 使用循环读取每个字符,并将其添加到该字符串变量中,直到遇到换行符为止。
3. 接下来,创建一个新的空字符串变量,用于存储逆置后的字符串。
4. 创建一个临时字符串变量,用于存储当前单词。
5. 使用循环遍历输入的字符串,当遇到空格或结束符时,将临时字符串变量中的内容逆置,并添加到新的字符串变量中。
6. 继续遍历直到字符串结束。
7. 最后,输出逆置后的字符串。
以下是一个简单的示例代码:
```python
input_str = input("请输入字符串:") # 读取输入的字符串
reverse_str = "" # 存储逆置后的字符串
temp_str = "" # 临时存储当前单词
for char in input_str:
if char != ' ' and char != '\n':
temp_str += char # 将字符添加到临时字符串中
else:
# 将临时字符串逆置并添加到新的字符串中
reverse_str += temp_str[::-1]
reverse_str += char
temp_str = "" # 清空临时字符串
# 添加最后一个单词(若有)
reverse_str += temp_str[::-1]
print(reverse_str) # 输出逆置后的字符串
```
这个代码会在用户输入完整的字符串后,对每个单词进行逆置,并输出逆置后的结果。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)