输入十个字符,统计其中的空格、回车和其它字符个数。c语言
时间: 2023-11-02 15:28:22 浏览: 31
实现:
#include <stdio.h>
int main()
{
char str[10]; //定义字符数组
int spaceCount = 0, newlineCount = 0, otherCount = 0; //统计空格、回车和其它字符的个数
printf("请输入十个字符:");
scanf("%s", str);
for(int i = 0; i < 10; i++)
{
if(str[i] == ' ') //判断是否为空格
{
spaceCount++;
}
else if(str[i] == '\n') //判断是否为回车
{
newlineCount++;
}
else //剩余的就是其它字符
{
otherCount++;
}
}
printf("空格个数:%d\n", spaceCount);
printf("回车个数:%d\n", newlineCount);
printf("其它字符个数:%d\n", otherCount);
return 0;
}
相关问题
输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数C语言
以下是统计字符类型个数的C语言代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
int letter = 0, space = 0, digit = 0, other = 0;
printf("请输入10个字符:\n");
for (int i = 0; i < 10; i++) {
c = getchar();
if (isalpha(c)) {
letter++;
} else if (isspace(c)) {
space++;
} else if (isdigit(c)) {
digit++;
} else {
other++;
}
}
printf("字母个数:%d\n空格或回车个数:%d\n数字个数:%d\n其他字符个数:%d\n", letter, space, digit, other);
return 0;
}
```
用c语言实现 输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。
您好,根据您的要求,我可以回答您的问题。以下是用 C 语言实现输入 10 个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数的代码:
```c
#include <stdio.h>
int main() {
char c;
int letter = , space = , digit = , other = ;
printf("请输入 10 个字符:\n");
for (int i = ; i < 10; i++) {
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
letter++;
} else if (c == ' ' || c == '\n') {
space++;
} else if (c >= '' && c <= '9') {
digit++;
} else {
other++;
}
}
printf("英文字母个数:%d\n", letter);
printf("空格或回车个数:%d\n", space);
printf("数字字符个数:%d\n", digit);
printf("其他字符个数:%d\n", other);
return ;
}
```
希望能够帮助您!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)