C程序以最低频率计数字符

问题描述 投票:1回答:2

我已编写此代码来查找频率最低的字符。

因此,输入“嗨,今天的天气如何,输出应该是

[频率最小的字母是s,频率是1。

但它显示

enter image description here

如何消除昏迷,我在这里的错误是什么

#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]='\0';
            }
          }
          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;
}
c arrays char c-strings counting
2个回答
0
投票

对于启动器,功能gets不安全,C标准不支持。而是使用标准的C函数fgets

通常,输入的字符串可能很大,而转换为小写的字符串中的字母可以在['a','z']范围内,因此没有必要将大数组声明为数组fre声明为类似。

int  fre[MAX];

由于您已经包含了标题<string.h>,所以手动计算输入字符串的长度是没有意义的。

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

要从计数中排除非字母字符,可以使用标头isalpha中声明的标准C函数<ctype.h>

请注意,通常输入的字符串不能包含字母。

这里是一个演示程序,显示了如何实现您的方法。

#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

0
投票

确实没有理由一次阅读多个字符。通常,这是尝试遵循的好模式。例如:

#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;                                                                  
} 
© www.soinside.com 2019 - 2024. All rights reserved.