Program to show the amount of uppercase shows only the amount of lowercase

Asked

Viewed 32 times

-2

I wrote a code to receive a char arrangement and return the amount of uppercase letters, but it’s doing the opposite ( returning the amount of lowercase ) and I don’t know why.

void Questao4 ()
{   
    //definir dado
    char* cadeia ;
    cadeia = (char*) malloc( 20* sizeof (char)); 

     int i = 0;

    //identificar
    printf ( "EXEMPLO0413 - Metodo03 - v1.0\n" );

    //declaracao e entrada
    do
    {
        printf ( "Entre com uma cadeia de caracteres ( 0 para sair )\n " );
        scanf ( " %c", &cadeia[i]);
        fflush (stdin);
        getchar();
        i++;
    }
    while ( cadeia[i-1] != '0' );

    //saida
    printf ( "Na cadeia de caracteres apresentada, ha' [%d] maiusculas !!\n" ,contamaiuscula ( cadeia ) );
}


int contamaiuscula ( char x[])
{
    //definir dado
    int numerodemaiusculas = 0;
    int i = 0;

    //contagem de maiusculas
    while ( x[i] != '\0')
    {
        if ( x[i] >= 'A' && x[i] >= 'Z' )
        {
            numerodemaiusculas ++;
        }
        i++;
    }

    return ( numerodemaiusculas );
}
  • I found...stupid mistake. (x[ i ] <= 'Z')

1 answer

2

that line is wrong in your code

 if ( x[i] >= 'A' && x[i] >= 'Z' )

just change and put like this:

if ( x[i] >= 'A' && x[i] <= 'Z' )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.