在c语言中假定输入的字符串中只包含字母和*。请编写程序,它的功能是将字符串中的*全部删除。例如,字符串中的内容为“****A*BC*DEF*G*******”删除*后,字符串中的内容应该为“ABCDEFG”。在编写函数时,不得使用C语言提供的字符串函数。
时间: 2023-12-04 10:41:14 浏览: 47
```c
#include <stdio.h>
void delete_star(char *str) {
char *p = str;
char *q = str;
while (*p != '\0') {
if (*p != '*') {
*q = *p;
q++;
}
p++;
}
*q = '\0';
}
int main() {
char str[] = "****A*BC*DEF*G*******";
delete_star(str);
printf("%s\n", str); // 输出:ABCDEFG
return 0;
}
```
--相关问题--:
1. 如何在C语言中判断一个字符串是否为空?
2. 如何
相关问题
用C语言:假定输入的字符串中只包含字母和号。编写程序只删除字符串前面的号
好的,以下是一个简单的 C 语言函数,可以实现你所描述的功能:
```c
#include <stdio.h>
#include <string.h>
char* remove_star_prefix(char* s) {
int i = 0;
while (i < strlen(s) && s[i] == '*') {
i++;
}
return s + i;
}
```
这个函数接受一个字符串作为参数,然后删除字符串前面的所有 * 号,并返回新的字符串。它使用了一个 while 循环来找到第一个非 * 字符的位置,然后返回从这个位置开始到字符串末尾的子串。
你可以把这个函数放到你的程序中,然后调用它来处理你的输入字符串。希望这可以帮助到你!
c语言写假定输入的字符串中只包含字母和*号。请编写函数 fun,它的功能是将字符串中的前导*号全部移到字符串的尾部。
以下是实现该功能的C语言代码:
```c
void fun(char *str) {
int len = strlen(str);
int i, j;
// 计算前导*号的数量
int count = 0;
for (i = 0; i < len; i++) {
if (str[i] == '*') {
count++;
} else {
break;
}
}
// 移动前导*号到字符串尾部
for (i = 0, j = count; j < len; i++, j++) {
str[i] = str[j];
}
for (i = len - count; i < len; i++) {
str[i] = '*';
}
}
```
该函数的思路是先计算前导*号的数量,然后将非*号字符向前移动,最后将前导*号移到字符串尾部。注意要使用字符串的长度来遍历字符串。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)