C语言有以下字符串:****A*BC*DEF****,删除字符串中间的星号,得到如下结果:****ABCDEF****
时间: 2023-09-05 11:08:43 浏览: 36
您好!可以使用C语言中的字符串处理函数来实现删除字符串中间的星号,具体步骤如下:
1. 遍历字符串,找到第一个不是星号的字符的位置,记为start。
2. 从start开始遍历字符串,找到第一个星号的位置,记为mid。
3. 从mid开始遍历字符串,找到第一个不是星号的字符的位置,记为end。
4. 将从start到mid-1的子串和从end到字符串结尾的子串拼接起来,即为删除中间星号后的结果。
下面是示例代码实现:
```c
#include <stdio.h>
#include <string.h>
void delete_star(char *str) {
int start = 0, mid = 0, end = 0;
int len = strlen(str);
// 找到第一个不是星号的字符的位置
while (start < len && str[start] == '*') {
start++;
}
// 从start开始遍历字符串,找到第一个星号的位置
mid = start;
while (mid < len && str[mid] != '*') {
mid++;
}
// 从mid开始遍历字符串,找到第一个不是星号的字符的位置
end = mid;
while (end < len && str[end] == '*') {
end++;
}
// 将从start到mid-1的子串和从end到字符串结尾的子串拼接起来
memmove(str + start, str + mid, end - mid);
str[start + end - mid] = '\0';
}
int main() {
char str[] = "****A*BC*DEF****";
delete_star(str);
printf("%s\n", str); // 输出结果为****ABCDEF****
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)