Problem in using char string

Asked

Viewed 54 times

1

Guys help me: the exercise I’m trying to do is 4. Write a program in which:

a) A character must be read that is one of the vowels of the alphabet and may be uppercase or lowercase.

b) a function is implemented in which to check which vowel has been read, print in the function itself one of the following messages, as the case may be:

1st vowel,

second vowel,

third vowel,

4th vowel,

5th vowel or

Another character

Can someone tell me what I’m doing wrong?

#include<stdlib.h>
#include<locale.h>

char VogaLida (char Vogal [], int TamanhoVetor);


int main (){

    setlocale(LC_ALL, "PORTUGUESE");

    char Vogal [] = {'a', 'e', 'i', 'o', 'u'};
    char VogalLida;

    printf("Informe uma vogal: ");
    gets(Vogal);

    VogalLida = VogaLida(Vogal, 5);

}

char VogaLida (char Vogal [], int TamanhoVetor)
{
    int i = 0;

    for (i = 0; i < 5; i++){

        if (i == Vogal[0]) {
            Vogal[0] = 'a' | 'A';
            printf("1ª Vogal\n");

            return 0;
        }
        else if (i == Vogal[1]){
            Vogal[1] = 'e' | 'E';
            printf("2ª Vogal\n");

             return 0;
        }

        else if (i == Vogal[2]){
            Vogal[2] = 'i' | 'I';
            printf("3ª Vogal\n");

             return 0;
        }


        else if (i == Vogal[3]){
            Vogal[3] = 'o' | 'O';
            printf("4ª Vogal\n");

             return 0;
        }

    }

        return 0;
}
  • 1

    If Christian’s answer solved your problem you can mark it as accepted. Otherwise you can give an answer of your own to explain the solution.

1 answer

1

Your program is bugged man, logic is in trouble.

  1. You are reading a character with the function gets() and placing on the vector that had its characters Vogal, and you only need to read a character.
  2. After you call the method VogalLida that checks the vowels but you use the i which is an integer number to make the comparisons within the block for.
  3. ...

Actually, there’s no way I can explain everything around here, so follow the reasoning:

1) According to its prinf() you just want to read a letter to check if it is a vowel. Then try the getc() and save in a variable char.

2)Do the function VogalLida(char vogal) with the following body:

void VogalLida(char vogal)
{
    switch(vogal)
    {
        case 'a': printf("1ª Vogal\n"); break;
        case ...;
        default: printf("Nao eh vogal\n");
    }
}

Now just implement the other cases, the way you implemented you used a lot of resources and ended up cluttering the logic.

  • Thanks for the reply Christian ! After I posted here my question I can solve and I saw that my logic was very wrong. Thanks anyway your willingness to help. Thanks.

  • All right, man, we’re here to help. But then take a look at Tour for you to understand a little better of Stackoverflow and how to signal your thanks with the answers, because then you help me too!

Browser other questions tagged

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