C程序计算出现频率最低的字符

C program to count the character with the lowest frequency

我写了这段代码来寻找出现频率最低的字符。

所以,输入"Hi, how is the weather todayy Dori",输出应该是

出现频率最低的字母是‘s’,出现频率为1。

但是显示

如何去除昏迷我这里的错误是什么

#include<stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000

int main()
{
    char str[MAX];
    int  fre[MAX],i,q,r,co=0,num;

    printf("The string : ");
    gets(str);

    for(q=0;str[q];q++);
    r=num=q;

    for(i=0;i<num;i++)
    {
        fre[i]=num;
        co=1;
        if(str[i])
        {

          for(q=i+1;q<num;q++)
          {

            if(tolower(str[i]) == tolower(str[q]))
         {
            {
                 co++;
                 str[q]='[=10=]';
            }
          }
          fre[i]=co;
        if(co<=r)
         r=co;

       }

        }
    }
    printf("The letter with the minimum frequency is");
    for(q=0;q<num;q++)
        {

            if(fre[q]==r)
            {
                 printf(" '%c' ",str[q]);
            }
       }

    printf("and the frequency is %d \n ",r);

    return 0;
}

对于初学者来说,函数 gets 是不安全的,并且不受 C 标准支持。而是使用标准 C 函数 fgets.

因为输入的字符串一般可以很大,而转换成小写的字符串中的字母可以在['a'、'z']范围内,所以没有意义声明一个大数组,如数组fre declared like.

int  fre[MAX];

因为您已经包含了 header <string.h> 那么手动计算输入字符串的长度是没有意义的。

for(q=0;str[q];q++);

要从计数中排除 non-letters 个字符,您可以使用在 header <ctype.h>.

中声明的标准 C 函数 isalpha

注意输入的字符串一般不能有字母

这是一个演示程序,展示了如何实施您的方法。

#include <stdio.h>
#include <ctype.h>

int main(void) 
{
    enum { MAX = 1000 };
    char s[MAX];
    size_t frequency[ 'z' - 'a' + 1] = { 0 };
    const size_t N = sizeof( frequency ) / sizeof( *frequency );

    printf( "The string: " );

    if ( fgets( s, MAX, stdin ) )
    {
        for ( const char *p = s; *p; ++p )
        {
            if ( isalpha( ( unsigned char )*p ) )
            {
                ++frequency[tolower( ( unsigned char )*p ) - 'a'];
            }
        }

        size_t min = 0;

        for ( size_t i = 0; i < N; i++ )
        {
            if ( frequency[i] != 0  && ( min == 0 || frequency[i] < min ) )
            {
                min = frequency[i];
            }
        }

        if ( min == 0 )
        {
            puts( "There ie no letters in the entered string." );
        }
        else
        {
            printf( "The letter with the minimum frequency is: " );
            for ( size_t i = 0; i < N; i++ )
            {
                if ( frequency[i] == min ) printf( "%c ", ( int )('a' + i ) );
            }

            printf( "\nand the frequency is %zu\n ", min );
        }
    }

    return 0;
}

程序输出可能看起来像

The string: Hi, how is the weather todayy Dor
The letter with the minimum frequency is: s 
and the frequency is 1

确实没有理由一次阅读一个以上的字符。一般来说,这是一个值得尝试和遵循的好模式。例如:

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

int main(void)                                                                     
{                                                                                  
        int c;                                                                     
        int fre[26] = {0};                                                         
        printf("The string : ");                                                   
        while( (c = getchar()) != EOF ) {                                          
                putchar(c);                                                        
                if( isalpha(c) ) {                                                 
                        fre[tolower(c) - 'a'] += 1;                                
                }                                                                  
        }                                                                          
        printf("The letter with the minimum frequency is");                        
        int idx = 0;                                                               
        int m = INT_MAX;                                                           
        for( int q = 0; q < 26; q++ ) {                                            
                if( fre[q] > 0 && fre[q] < m ) {                                   
                        idx = q;                                                   
                        m = fre[q];                                                
                }                                                                  
        }                                                                          
        printf(" '%c', with frequency %d\n", idx + 'a', fre[idx]);                 
        return 0;                                                                  
}