程序应用字符数组存储从键盘输入的一行英文(100个以内),统计各个字符出现的次数。

输入格式:
请输入一行字符(字符个数小于100)。

输出格式:
统计个字符的数量,一行一个字符地按照以下格式输出数量不为0的字符及其数量:

#include <stdio.h>
#include <string.h>

#define MAX_LEN 100

int main() {
    char str[MAX_LEN];
    int counts[128] = {0}; 

    fgets(str, MAX_LEN, stdin); 

    int len = strlen(str);
    for (int i = 0; i < len; i++) {
        char c = str[i];
        if (c == '\n') break;
        counts[(int)c]++;
    }

    for (int i = 0; i < 128; i++) {
        if (counts[i] != 0) {
            printf("%c: %d\n", (char)i, counts[i]); 
        }
    }

    return 0;
}

https://blog.csdn.net/qq_43444398/article/details/85882834
或者使用这种方法:

#include <stdio.h>
#include <string.h>

int main() {
    char str[101];
    int len, count[26] = {0};

    scanf("%s", str);
    len = strlen(str);

    for (int i = 0; i < len; i++) {
        count[str[i] - 'a']++;
    }

    for (int i = 0; i < 26; i++) {
        if (count[i] != 0) {
            printf("%c %d\\n", i + 'a', count[i]);
        }
    }

    return 0;
}


自己创个样式输入就看得到了

供参考:

#include <stdio.h>
int main()
{
    int  cnt[128] = { 0 }, i = 0;
    char str[100];
    gets(str);
    while (str[i]) cnt[str[i]]++, i++;
    for (i = 0; i < 128; i++) {
        if (cnt[i])
            printf("%c %d\n", i, cnt[i]);
    }
    return 0;
}

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7592677
  • 你也可以参考下这篇文章:读取文件,每行不超过100个字符,输出每行中字母最多的单词的字母数
  • 除此之外, 这篇博客: 【 英文单词排序 】 本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。中的 输出格式: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 输出为排序后的结果,每个单词后面都额外输出一个空格。
    输入样例:
    blue
    red
    yellow
    green
    purple
    #
    输出样例:
    red blue green yellow purple
    #include <stdio.h>
    #include <string.h>
    main()
    {
        char str[20][10],t[20],str1[10];
        int i,j,n=0;
        while(1)
    	{
        	scanf("%s",str1);
        	if(str1[0]=='#')
    	    {
        		break;
        	}
            else
    		{
            strcpy(str[n],str1);
            n++;
            }
        }
        for(i=0;i<n-1;i++)
        	for(j=0;j<n-i-1;j++)
    	    {
                if(strlen(str[j])>strlen(str[j+1]))
    		    {
                   strcpy(t,str[j]);
                   strcpy(str[j],str[j+1]);
                   strcpy(str[j+1],t);
                }
            }
        for(i=0;i<n;i++)
    	{
            printf("%s ",str[i]);
        }
    }

  • 您还可以看一下 刘欢老师的从前端到后台,开发一个完整功能的小程序课程中的 搭建与设置服务器域名小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    首先可以使用字符数组 char str[100] 存储从键盘输入的一行英文,长度不超过 100 个字符。

    接着可以使用一个整型数组 int cnt[128] 统计每个字符出现的次数,其中下标为字符的 ASCII 码值,遍历字符数组,对应位置的计数器加 1。

    最后遍历整型数组,输出不为 0 的字符及其数量即可。以下是示例代码:

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main() {
        char str[100];
        int cnt[128] = {0}; // 计数器数组初始化为 0
        cin.getline(str, 100); // 输入一行字符串
        int len = strlen(str); // 字符串长度
        for (int i = 0; i < len; i++) {
            if (str[i] != ' ') { // 不统计空格
                cnt[str[i]]++;
            }
        }
        for (int i = 0; i < 128; i++) {
            if (cnt[i] != 0) {
                cout << (char)i << ": " << cnt[i] << endl;
            }
        }
        return 0;
    }