Home > Enterprise >  what's wrong with my program,so only the letter i can't be counted?
what's wrong with my program,so only the letter i can't be counted?

Time:10-08

So here, I want to solve one of the problems in my basic programming class. So, the problem is, we should count the vowel letter and print how many A/a, I/i, U/u, E/e, and O,o letters are in a string. In this case, I used an ASCII for checking the letter. All the programs run so well but I found one problem. My program cannot count the I/i letter. There always be counted zero although I input an "I" letter. Can you help me fix it?

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

int main () {

    int len, a = 0, i = 0, u = 0, e = 0, o = 0;
    char str[150];
    scanf("%[^\n]s", &str);
    len = strlen(str);

    for(int i = 0; i <= len; i  ){
        if(str[i] == 65 || str[i] == 97){
            a  ;
            continue;
        }
        if(str[i] == 73 || str[i] == 105){
            i  ;
            continue;
        } 
        if(str[i] == 85 || str[i] == 117){
            u  ;
            continue;
        } 
        if(str[i] == 69 || str[i] == 101){
            e  ;
            continue;
        } 
        if(str[i] == 79 || str[i] == 111){
            o  ;
            continue;
        } 
    }

    printf("A/a : %d\n", a);
    printf("I/i : %d\n", i);
    printf("E/e : %d\n", e);
    printf("U/u : %d\n", u);
    printf("O/o : %d\n", o);

}

CodePudding user response:

You're shadowing the variable i in your for loop. Change the loop index to j, for example, and it'll work.

A few other things:

  1. Instead of using magic numbers such as 65 and 97 I'd suggest using A and a.
  2. Using else if would be preferable than many if statements and continue.

CodePudding user response:

For starters this call of scanf

scanf("%[^\n]s", &str);

is incorrect. For example the expression &str has the type char( * )[150] instead of the expected argument of the type char *.

You should write

scanf("9[^\n]", str);

In this for loop

for(int i = 0; i <= len; i  ){

the declared variable i hides the variable with the same name declared before the for loop.

Also there is no sense to use the condition i <= len instead of i < len.

Though using the function strlen is redundant.

And do not use magic numbers like for example 65. This makes the program unreadable and moreover the program will not work if the system supports the EBCDIC character table.

Instead of the if statements with the continue statement it will be much better to use a switch statement.

Your program can look the following way

#include <stdio.h>

int main( void ) 
{
    int a = 0, i = 0, u = 0, e = 0, o = 0;
    char str[150];

    scanf( "9[^\n]", str );

    for ( const char *p = str; *p != '\0';   p )
    {
        switch ( *p )
        {
        case 'a': case 'A':    
            a  ;
            break;

        case 'i': case 'I':
            i  ;
            break;

        case 'u': case 'U': 
            u  ;
            break;

        case 'e': case 'E':
            e  ;
            break;

        case 'o': case 'O':
            o  ;
            break;;
        } 
    }

    printf( "A/a : %d\n", a );
    printf( "I/i : %d\n", i );
    printf( "E/e : %d\n", e );
    printf( "U/u : %d\n", u );
    printf( "O/o : %d\n", o );
}

To make the program more safer you could also check whether the input of a string was successful as for example

    if ( scanf( "9[^\n]", str ) == 1 )
    {
        for ( const char *p = str; *p != '\0';   p )
        {
        //... 
  • Related