c语言题目, 输入任意长度的字符串,输出这个字符串中只出现一次的第一个字符,并输出这个字符串中出现

c语言题目, 输入任意长度的字符串,输出这个字符串中只出现一次的第一个字符,并输出这个字符串中出现最多次的字符,写出c语言程序。

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char ch,mostch='\0';
    int max=0,first=0,index=-1;
    static firstch[255];
    static int num[255];
    while((ch=fgetc(stdin))!=EOF)
    {
        num[ch]++;
        if(num[ch]==1)
        firstch[++index]=ch;
        if(num[ch]>1&&ch==firstch[first]) 
             ++first;
        if(num[ch]>max)
        {
            max=num[ch];
            mostch=ch;
        }     
    }
    if(index>=0) {
    if(first<=index)
        printf("only occur  1 times char in begin is: %c\n",firstch[first]);
    printf("occur most char is :%c\n",mostch);}
    return 0;
}

追问

追答#include <stdio.h>
#include <stdlib.h>
#define LEN 255
int main()
{
    char ch,mostch='\0';
    int max=0,i=0;
    int num[LEN]={0};
    char firstch[LEN];
    while((ch=fgetc(stdin))!='\n'&&ch!=EOF)
    {
        num[ch]++;
        firstch[i++]=ch;
        if(num[ch]>max)
        {
            max=num[ch];
            mostch=ch;
        }     
    }
    if(max!=0)
    {    
        for(i=0;i<LEN;i++)
            if(num[firstch[i]]==1)
            {
                printf("only occur 1 times char in begin is: %c\n",firstch[i]);
                break;}
            printf("occur most char is :%c\n",mostch);}
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-23
输入的字符串只含有字母 A-Z 以及 a-z 么,如果是的话就好做了
第2个回答  2015-03-04
你等等,我明天写后就发给你追问

好的

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网