输入格式: 请输入一行字符(字符个数小于100)。 输出格式: 统计个字符的数量,一行一个字符地按照以下格式输出数量不为0的字符及其数量: <字符>--><数量>用C语言来写
时间: 2023-12-06 19:44:54 浏览: 46
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count[128] = {0}; // ASCII 码表中共有 128 个字符,所以开一个大小为 128 的数组来统计数量
printf("请输入一行字符(字符个数小于100):\n");
fgets(str, 100, stdin);
for (int i = 0; i < strlen(str); i++) {
char c = str[i];
count[c]++;
}
for (int i = 0; i < 128; i++) {
if (count[i] != 0) {
printf("%c-->%d\n", i, count[i]);
}
}
return 0;
}
```
相关问题
c语言请输入一行字符(字符个数小于80) 输出反序存放的字符串。
下面是一个简单的 C 代码实现:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[80];
int len, i;
printf("请输入一行字符(字符个数小于80):");
fgets(str, sizeof(str), stdin);
len = strlen(str) - 1; // 减去换行符
printf("反序存放的字符串为:");
for (i = len - 1; i >= 0; i--)
{
printf("%c", str[i]);
}
return 0;
}
```
代码的思路是先读入一行字符串,然后计算字符串长度并逆序输出每个字符。注意,fgets 函数会读入换行符,因此要在计算长度时减去 1。
Python统计数字字符个数:输入一行字符,统计出其中数字字符的个数。
可以使用Python中的isdigit()函数来判断一个字符是否为数字字符,然后遍历输入的字符串,统计其中数字字符的个数。具体实现如下:
```python
string = input("请输入一行字符:")
count = 0
for char in string:
if char.isdigit():
count += 1
print("数字字符个数为:", count)
```
运行程序后,会提示你输入一行字符,输入后程序会统计其中数字字符的个数并输出。
例如,输入字符串"Hello12345",程序会输出数字字符个数为5。
注意,isdigit()函数只能判断单个字符是否为数字字符,如果需要统计一个字符串中数字字符的个数,需要遍历字符串中的所有字符并判断。
相关推荐
![none](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)