c语言统计字符串中的字母数
时间: 2024-06-13 17:07:18 浏览: 9
以下是两种C语言统计字符串中字母数的方法:
1. 指针法
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int count = 0;
printf("请输入一个字符串:");
gets(str);
char *p = str;
while (*p != '\0')
{
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
{
count++;
}
p++;
}
printf("字母个数为:%d\n", count);
return 0;
}
```
2. 数组法
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int count = 0;
printf("请输入一个字符串:");
gets(str);
for (int i = 0; i < strlen(str); i++)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
count++;
}
}
printf("字母个数为:%d\n", count);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)