Check if it is vowel or consonant and if it is lowercase or uppercase without using ready-made functions

Asked

Viewed 218 times

2

I am developing two functions in C language, the first to check whether a letter is a vowel or a consonant, without using any ready function. The first is working.

The second function checks whether the letter is uppercase or lowercase, but does not work for all letters. For example, the letter "a" does not show output.

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

char letras[52] = {'A','E', 'I','O','U',
                    'a','e', 'i','o','u',
                    'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z',
                    'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'
};

void minusculaMaiuscula(char letra){
    for (int i = 0; i < 52; i++){
        if( (i < 5 | (i > 10 & i < 31)) & (letra == letras[i]) ){
            printf("Maiuscula");
            break;
        }
        else if ( (i > 5 & i < 31) & letra == letras[i] ){
            printf("Minuscula");
            break;
        }
    }        
}

void vogalConsoante(char letra){
    for (int i = 0; i < 52; i++){
        if(i < 10 & letra == letras[i] ){
            printf("Vogal");
            break;
        }
        else if (i > 5 & letra == letras[i] ){
            printf("Consoante");
            break;
        }
    }
}


int main()
{
    char letra = 'a';
    minusculaMaiuscula(letra);
    
    return 0;
}

Where is the error in my function minusculaMaiuscula(char letra) and, how to improve these functions so that the code is more understandable and with less instructions?

1 answer

5


You don’t actually need this array of letters, just check if the char is between the letters "a" and "z":

void minusculaMaiuscula(char letra) {
    if ('a' <= letra && letra <= 'z') {
        printf("Minúscula\n");
    } else if ('A' <= letra && letra <= 'Z') {
        printf("Maiúscula\n");
    } else {
        printf("Não é letra\n");
    }
}

If you do not want to print anything if it is not a letter, just remove the last else.


If you really want to use the array, the problem with your code is that you haven’t tested the case where i is equal to 5 (you test whether it is smaller or larger, but when it is equal, it does not enter any of the conditions). I did not evaluate the other conditions, but in fact we can simplify them, as we will see below.

You can also change operators | and &, who are actually operators bitwise. Instead, you can use the logical operators "or" and "and" (which are respectively || and &&).

The advantage of using && and || is that they possess the characteristic of being short-Circuit and assess all conditions only if necessary (for example, a && b is only true if both conditions a and b are also true, so if the first is false, it does not even evaluate the second).

void minusculaMaiuscula(char letra) {
    for (int i = 0; i < 52; i++) {
        if (letra == letras[i]) { // só verifico o índice se a letra for igual
            if ((i < 5) || (10 <= i && i <= 30)) {
                printf("Maiúscula\n");
            } else {
                printf("Minúscula\n");
            }
            break; // se já encontrei a letra, não tem porque continuar procurando
        }
    }
}

But I still find this loop unnecessary: there is no reason to iterate through the letters to test the value of the letter directly.


The same goes for the other function:

// sem o array
void vogalConsoante(char letra) {
    if (('a' <= letra && letra <= 'z') || ('A' <= letra && letra <= 'Z')) { // se não for letra, nem testo
        switch(letra) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                printf("Vogal\n");
                break;
            default:
                printf("Consoante\n");
        }
    }
}

//--------------------------
// outra opção, sem o array
void vogalConsoante(char letra) {
    // se for maiúscula, converte para minúscula
    if ('A' <= letra && letra <= 'Z') {
        letra += 32;
    }
    if (('a' <= letra && letra <= 'z')) { // se não for letra, nem testo
        switch(letra) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                printf("Vogal\n");
                break;
            default:
                printf("Consoante\n");
        }
    }
}

//--------------------------
// com o array
void vogalConsoante(char letra) {
    for (int i = 0; i < 52; i++) {
        if (letra == letras[i]) { // só verifico o índice se a letra for igual
            if (i < 10) {
                printf("Vogal\n");
            } else {
                printf("Consoante\n");
            }
            break; // se já encontrei a letra, não tem porque continuar procurando
        }
    }
}

Browser other questions tagged

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