Problem in using char string

1

Galera helps me: the exercise I'm trying to do is 4. Write a program where:

a) Read a character that must be one of the alphabet vowels, which may be uppercase or lowercase.

b) If a function is found in which you check which vowel has been read, print one of the following messages, depending on the case:

  

1st vowel,

     

2nd vowel,

     

3rd vowel,

     

4th vowel,

     

5th vowel or

     

Another character

Can anyone 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;
}
    
asked by anonymous 20.06.2016 / 17:02

1 answer

1

Your program is bugged, the logic is in trouble.

  • You are reading a character with the function gets() and placing in the vector that had its Vogal characters, you only need to read one character.
  • After you call the VogalLida method that checks the vowels however you use i which is an integer to make the comparisons inside the for block.
  • ...
  • Actually, there is no way for me to explain everything here, so follow the reasoning:

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

    2) Make the VogalLida(char vogal) function 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 many resources and ended up embolando the logic.

        
    20.06.2016 / 17:56