c语言在主函数中输入一个由星号和字母组成的字符串,其中开头和结尾是若干星号,中间是字母和星号,例如:******A*B**CD***。定义子函数del_middle_star( ),其功能是删除字符串中间所有的星号。
时间: 2023-12-06 08:05:28 浏览: 50
以下是C语言中删除字符串中间所有星号的代码:
```c
#include <stdio.h>
void del_middle_star(char *s) {
char *p = s, *q = s;
while (*p != '\0') {
if (*p != '*') {
*q++ = *p;
}
p++;
}
*q = '\0';
}
int main() {
char s[81] = "***ABC*DFG*HJ";
// gets(s);
del_middle_star(s);
puts(s); return 0;
}
```
相关问题
C语言 写一数组 使输入一个字符串按反序存放,在主函数中输入和输出字符串
好的,下面是一个简单的C语言程序,可以实现输入一个字符串并按反序存放,然后在主函数中输出反序后的字符串。
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
int main() {
char str[MAX_LEN];
printf("请输入一个字符串:");
fgets(str, MAX_LEN, stdin);
int len = strlen(str) - 1; // 减一是为了去掉字符串末尾的换行符
char rev_str[MAX_LEN];
for (int i = 0; i < len; i++) {
rev_str[i] = str[len - 1 - i];
}
rev_str[len] = '\0'; // 在反序字符串末尾添加一个空字符,以便输出
printf("反序后的字符串为:%s\n", rev_str);
return 0;
}
```
程序的运行结果如下:
```
请输入一个字符串:Hello World!
反序后的字符串为:!dlroW olleH
```
注意,这个程序对输入的字符串长度有限制,不能超过预定义的最大长度 `MAX_LEN`。如果需要处理更长的字符串,可以使用动态内存分配来实现。
在c语言中统计一个字符串中字母,空格,数字和其他字符的个数,主函数负责输入和输出。
好的,这里是一个示例代码,可以实现你所需的功能:
```c
#include <stdio.h>
#include <ctype.h>
void count_chars(char *str, int *letters, int *spaces, int *digits, int *others) {
while(*str) {
if(isalpha(*str)) {
(*letters)++;
} else if(isspace(*str)) {
(*spaces)++;
} else if(isdigit(*str)) {
(*digits)++;
} else {
(*others)++;
}
str++;
}
}
int main() {
char str[100];
int letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入一个字符串:\n");
fgets(str, 100, stdin);
count_chars(str, &letters, &spaces, &digits, &others);
printf("字母个数:%d\n", letters);
printf("空格个数:%d\n", spaces);
printf("数字个数:%d\n", digits);
printf("其他字符个数:%d\n", others);
return 0;
}
```
在这个程序中,`count_chars()` 函数用于统计一个字符串中字母,空格,数字和其他字符的个数。`main()` 函数负责输入和输出。当你输入一个字符串时,程序将会输出该字符串中字母,空格,数字和其他字符的个数。
希望对你有帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)