First, let’s start with this:
if(letra == palavra[i]){
return 1 + ocorrencias(palavra[i++], letra);
}else{
return ocorrencias(palavra[i++], letra);
}
You can delete this repetition of the recursive call you have on if
and in the else
thus:
int a = (letra == palavra[i]);
int b = ocorrencias(palavra[i++], letra);
return a + b;
And this in turn can be simplified in this:
return (letra == palavra[i]) + ocorrencias(palavra[i++], letra);
But that’s not your problem yet. To understand your problem, let’s see what the value of i
:
int i;
That is to say, the variable i
is worthless! This is the main reason why it doesn’t work. Soon, palavra[i]
and palavra[i++]
are not expressions that will have meaning.
To fix this, what you’re trying to do is look at the first letter of the word with the letra == palavra[i]
and recursively pass the remainder of the word with palavra[i++]
. Now, the first letter of the word is the one at zero, and so you use palavra[0]
instead of palavra[i]
. For the rest of the word, you take the address of the second character, i.e., &(palavra[1])
. So this code stays like this:
return (letra == palavra[0]) + ocorrencias(&(palavra[1]), letra);
And also note that now, the variable i
is no longer used and can be deleted.
However, this still doesn’t work because the part that looks at the end of the string is still wrong:
if(letra == '\0'){
return 0;
}
This checks whether the letter given as input is an empty string. Now, this does not check at all if the word has already finished. So what you wanted was this:
if (palavra[0] == '\0') return 0;
And its function ocorrencias
gets like this:
int ocorrencias(char palavra[], char letra) {
if (palavra[0] == '\0') return 0;
return (letra == palavra[0]) + ocorrencias(&(palavra[1]), letra);
}
Here’s your complete code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
int ocorrencias(char palavra[], char letra) {
if (palavra[0] == '\0') return 0;
return (letra == palavra[0]) + ocorrencias(&(palavra[1]), letra);
}
int main() {
char palavra[MAX];
char letra;
scanf("%s %c", palavra, &letra);
int ocorre = ocorrencias(palavra, letra);
printf("%d", ocorre);
return 0;
}
See here working on ideone.
Ah, you could still reduce the function ocorrencias
a little more leaving it in a single line, but then you may find that this is already exaggerated:
int ocorrencias(char palavra[], char letra) {
return palavra[0] == '\0' ? 0 : (letra == palavra[0]) + ocorrencias(&(palavra[1]), letra);
}
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero