c语言输入一个字符串,过滤此串,只保留串中的字母字符,并统计新生成串中包含的字母个数。函数接口定义: 1/1 1 fun ( char * ptr );//'或 fun ( char ptr []); ptr 由主函数输入的字符串传入。
时间: 2024-05-29 22:08:26 浏览: 20
void fun(char *ptr) {
int count = 0; // 统计字母个数
int len = strlen(ptr); // 获取字符串长度
for (int i = 0; i < len; i++) {
if (isalpha(ptr[i])) { // 如果是字母字符
ptr[count] = ptr[i]; // 将其放到新串中
count++; // 统计新串中的字母个数
}
}
ptr[count] = '\0'; // 在新串末尾加上字符串结束符
}
相关问题
c语言,输入一个字符串统计字符串中大写辅音字母个数并输出
下面是一个简单的 C 代码实现,可以实现输入一个字符串并统计其中大写辅音字母的个数并输出:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int count = 0;
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
for (int i = 0; i < strlen(str); i++) {
if (isupper(str[i]) && strchr("AEIOU", str[i]) == NULL) {
count++;
}
}
printf("大写辅音字母的个数为:%d\n", count);
return 0;
}
```
代码中使用 fgets 函数从标准输入读取一个字符串,然后使用 for 循环遍历字符串中的每一个字符,如果该字符是大写字母并且不是元音字母,则将计数器 count 加一。最后输出计数器的值即为大写辅音字母的个数。
c语言输入两组字符串并统计字符串中字母与数字的总个数
以下是一个C语言程序,可以输入两个字符串并统计字符串中字母与数字的总个数:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str1[100], str2[100];
int letter_count = 0, digit_count = 0;
int i;
printf("请输入第一个字符串:");
scanf("%s", str1);
printf("请输入第二个字符串:");
scanf("%s", str2);
for (i = 0; i < strlen(str1); i++) {
if (isalpha(str1[i])) { // 判断是否为字母
letter_count++;
} else if (isdigit(str1[i])) { // 判断是否为数字
digit_count++;
}
}
for (i = 0; i < strlen(str2); i++) {
if (isalpha(str2[i])) { // 判断是否为字母
letter_count++;
} else if (isdigit(str2[i])) { // 判断是否为数字
digit_count++;
}
}
printf("两个字符串中字母的总个数为:%d\n", letter_count);
printf("两个字符串中数字的总个数为:%d\n", digit_count);
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![md](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)
![](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)