photosynthesis_bro
photosynthesis_bro

Reputation: 81

How to print strings that have more consonants than vowels?

Enter the array of 20 strings. Make program that prints out strings which have more consonants than vowels and in which letter 'r' is repeated at least 3 times.

I belive that the problem is in my if loops, but somehow i fail to understand why it does not work properly. It prints every string I enter.

This is the code i wrote:

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


    int main(void) {

    char string[20][50];
    int i, j;
    int vowels=0;
    int consonants=0;
    int repeated_r=0;

    printf("Enter the array of 20 strings:\n");

    for(i=0;i<20;i++){
        gets(string[i]);
        }

    for(i=0;i<20;i++){
        for(j=0;j<50;j++){

            if(string[i][j] == 'r'){
                repeated_r++;
            }
            else if(string[i][j] == 'a' || string[i][j] == 'e' || string[i][j] == 'i' || string[i][j] == 'o' || string[i][j] == 'u'){
                vowels++;
            }
            else{
                consonants++;
            }

        }
        if(consonants>vowels && repeated_r>=3){

            fflush(stdin);
            puts(string[i]);
        }
    }


    return 0;
}    

Upvotes: 1

Views: 306

Answers (3)

Shreya Birthare
Shreya Birthare

Reputation: 45

there is one more problem in your code, ypu're using gets. It'll also include white spaces so say if your string is "_ _ rrrbaeeeeiou _ ". It will print this string but actually this string should not have been printed. Note that "" means a blank space. According to your code, "_" will be counted in consonants and even though there are 4 consonants in your string(3 r and 1 b) and 8 vowels, it will print this string as output since the blank spaces will be counted as consonants. Also in your code r is not counted as consonant

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

You are not resetting the initial values of the variables in the outer loop

int vowels=0;
int consonants=0;
int repeated_r=0;

Also the condition in the inner loop

for(j=0;j<50;j++){
        ^^^^

is not correct because in this case there is an access to memory beyond stored strings in the array.

The letter 'r' is not counted as a consonant.

Take into account that the function gets is not a standard C function that is it is not supported by the C Standard any more.

And this call

fflush(stdin);

has undefined behavior.

I can suggest the following solution as shown in the demonstrative program below.

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

int main( void ) 
{
    enum { M = 3, N = 50 };
    char s[M][N];

    for ( size_t i = 0; i < M; i++ )
    {
        fgets( s[i], N, stdin );
        s[i][strcspn( s[i], "\n" )] = '\0';
    }

    const char *vowels = "aeiou";
    const char r = 'r';

    for ( size_t i = 0; i < M; i++ )
    {
        size_t repeated_r = 0;
        size_t vowels_count = 0;
        size_t n = strlen( s[i] );

        for ( size_t j = 0; j < n; j++ )
        {
            repeated_r += s[i][j] == r;
            vowels_count += strchr( vowels, s[i][j] ) != NULL;
        }

        if ( repeated_r >= 3 && vowels_count < n - vowels_count )
        {
            puts( s[i] );
        }
    }

    return 0;
}

If to enter the following strings

    Hello World
    errors
    photosynthesis_bro

then the program output might look like

errors

Upvotes: 1

kiran Biradar
kiran Biradar

Reputation: 12742

You need to reset the counters after processing each string.

And don't use gets use fgets instead.

for(i=0;i<20;i++){
    for(j=0;j<50;j++){

        if(string[i][j] == 'r'){
            repeated_r++;
        }
        else if(string[i][j] == 'a' || string[i][j] == 'e' || string[i][j] == 'i' || string[i][j] == 'o' || string[i][j] == 'u'){
            vowels++;
        }
        else{
            consonants++;
        }

    }
    if(consonants>vowels && repeated_r>3){

        fflush(stdin);
        puts(string[i]);
    }

   //Reset the counters
    consonants =0;
    vowels =0;
    repeated_r =0;
  }
 }

Also note that in your current code r is not considered as consonant.

Upvotes: 2

Related Questions