简体   繁体   中英

C program to count the character with the lowest frequency

I have written this code for finding the character with the minimum frequency.

So, giving in input "Hi, how is the weather todayy Dori", the output should be

The letter with the minimum frequency is 's' and the frequency is 1.

But it shows

在此处输入图像描述

How to remove the that coma what is my mistake 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;
}

For starters the function gets is unsafe and is not supported by the C Standard. Instead use the standard C function fgets .

As an entered string in general can be very big while letters in the string converted to the lower case can be in the range of ['a', 'z'] then there is no sense to declare such a big array as the array fre declared like.

int  fre[MAX];

As you already included the header <string.h> then there is no sense manually to calculate the length of the entered string.

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

To exclude non-letters characters from counting you can use the standard C function isalpha declared in the header <ctype.h> .

Pay attention to that in general the entered string can have no letters.

Here is a demonstrative program that shows how your approach can be implemented.

#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 program output might look like

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

There's really no reason to read more than one character at a time. In general, that's a good pattern to try and follow. eg:

#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;                                                                  
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM