Char array problem to find out if the input is vowel, consonant and uppercase or lowercase

Asked

Viewed 221 times

1

I’m trying to get the user to type a symbol on input and the program recognizes whether the symbol typed was a vowel, an uppercase vowel, depending on the uppercase or other character.

For this I am comparing a string input with the arrays filled with certain vowels, consonants, etc., but prompt runs up to the input, and as soon as I insert it closes, I’m probably missing the syntax because I don’t know the C language very well.

Follows the code:

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

int main()
{

char vowels[] = {'a', 'e', 'i', 'o', 'u'},

upperVowels[] = {'A', 'E', 'I', 'O', 'U'},

consonants[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'},

upperConsonants[] = {'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'},

input;

    printf("Enter the symbol:\n");
    scanf("%c",&input);

    if (strcmp(input, vowels) == 0){
        printf("vowels!");
    } else if (strcmp(input, upperVowels) == 0){
        printf("Upper vowels!");
    } else if (strcmp(input, consonants) == 0){
        printf("Consonants!");
    } else if (strcmp(input, upperConsonants) == 0){
        printf("Upper Consonants!");
    } else {
        printf("You entered another character!");
    }

    return 0;
}
  • 1

    In your vowels, upperVowels, consonants, and upperConsonants definitions you have not defined strings but an array of characters (a string is a character array with the ' 0') terminator and therefore cannot use the <string function. h> as you did. Also your input variable is defined as a single character.

  • I get it, thank you!

1 answer

0


There are some problems there, one of them is the use of the wrong function and the other is the fact that it has not created strings and yes arrays of charss without a terminator, so are not strings. If you want to know the existence of a character in a string then you must use strchr(). This function returns the position (address) from which you have the character or a null if you find nothing.

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

int main() {
    char input, vowels[] = "aeiou", upperVowels[] = "AEIOU", consonants[] = "bcdfghjklmnopqrstuvwxyz", upperConsonants[] = "BCDFGHJKLMNPQRSTVWXYZ";
    printf("Enter the symbol: ");
    scanf("%c", &input);
    if (strchr(vowels, input) != 0) printf("vowels!");
    else if (strchr(upperVowels, input) != 0) printf("Upper vowels!");
    else if (strchr(consonants, input) != 0) printf("Consonants!");
    else if (strchr(upperConsonants, input) != 0) printf("Upper Consonants!");
    else printf("You entered another character!");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Man you helped me so much, really, thank you so much!!!

Browser other questions tagged

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