Recursive program question for writing vowels in C

2
  

Make a recursive function written in the programming language C with two parameters: a string and the initial position that should be considered in the search. The function must write the vowels found in the string from the initial position in order contrary to the order that appear in the string, the other characters should not be written. Repeat commands should not be used. Examples of calls and typed characters:

     
  • escrevevogais("aeiou",0) should write == > uoiea
  •   
  • escrevevogais("aeiou",2) should write == > uoi
  •   
  • escrevevogais("programa",0) should write == > aao
  •   
  • escrevevogais("abcd",1) should not write anything
  •   

How to do it?

Here is the code I tried:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int escrevevogais(char s[50],int i){
    if(i==0){
        return 0;
    }
    else{
        if((s[i]=='a')||(s[i]=='e')||(s[i]=='i')||(s[i]=='o')||(s[i]=='u')){
            printf("%s",s[i]);
        }
        int x=escrevevogais(s,i-1);
    }
}
int main(){
    char s[50];
    gets(s);
    int x=escrevevogais(s,strlen(s));
    }
    
asked by anonymous 04.07.2018 / 01:58

2 answers

2

First, the int return of the escrevevogais function is useless. Therefore, you may want to change to void .

Second, do not use gets . I'll talk more about this in these other answers: 1 , #

Third, with each recursive call, it is necessary to advance a letter in the word. That way, the deeper the recursion, the further away from the beginning of the string you are. This means that with each recursive call, i must be increased by 1. However, you were decreasing rather than increasing. The word never changes, so the first parameter is always the same.

Fourth, you can put the recursion before or after checking if the letter is a vowel:

  • If you put the recursion after verifying the letter, it will visit the letters one by one by going to the end of the string, stacking the recursive calls and then unmasking all the calls in reverse order, and as a result, the letters will appear in the order.

  • If you put the recursion before verifying the letter, it will stack the recursive calls to the end and as you uncrow them in reverse order, visit the letters in reverse order as well.

    li>

So you should put the recursion before verifying the letter.

Fifth, the first position of the string is zero. This means that giving return when i is zero is not the right approach.

Sixth, to write entire strings, use "%s" in printf . To write isolated characters within strings, use "%c" . The variable s is a sstring, so s[i] is an isolated character and must be written with "%c" .

I see that the main problem in your attempt is that you considered that i is decreasing from strlen(s) to zero, and the approach shown by the given examples is the opposite, i is growing until arrive at the end of the string (where there is the null terminator).

Considering all this, your code should look like this:

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

void escrevevogais(char s[50], int i) {
    if (s[i] == 0) return;
    escrevevogais(s, i + 1);
    if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') {
        printf("%c", s[i]);
    }
}

int main() {
    char s[50];
    fgets(s, 50, stdin);
    escrevevogais(s, 0);
}

The program does not consider uppercase vowels, but this is easy to fix.

See here working on ideone.

    
04.07.2018 / 04:30
0

Just complementing the response of Mr Victor Stafusa. If you want to enter words with capital letters and lowercase suffices write the function as follows:

void escreverVogais(char s[],int i){

if(s[i] == 0){
    return;
}
else{
    escreverVogais(s,i + 1);
    if((s[i]=='a')||(s[i]=='e')||(s[i]=='i')||(s[i]=='o')||(s[i]=='u')){
        printf("%c",s[i]);
    }
    else if((s[i]=='A')||(s[i]=='E')||(s[i]=='I')||(s[i]=='O')||(s[i]=='U')){
        printf("%c",s[i]);
     } // if else interno
   } // if else externo

 }
    
22.07.2018 / 01:49