In its implementation it has the logic that goes through the letters of the main sentence, but the logic that sees if each letter is equal to the letters of the second string is missing.
Trying to make the most of your logic can do so:
void str_conteudo( char str1[], char str2[]) {
int i=0,j, k;
int y = -1; //y começa a -1, pois estava sem valor de inicio
while(str1[i] != '\0') { // enquanto a string principal não termina
if (str2[0] == str1[i]) { //se a letra em que vai é igual à 1a letra da substring
//percorrer enquanto ambas as letras são iguais e até ao fim da substring
for (j=i+1, k=1; str1[j]!='\0' && str2[k]!='\0' && str1[j] == str2[k];++j,k++);
if (str2[k] == '\0'){ //se a segunda foi até ao fim então achou
y = i;
break;
}
}
i++;
}
if(y>=0) // >= 0 e não > 0
printf("Indice do primeiro caractere que contem a string 2: %d \n", y);
else
printf("A string 2 nao esta contida na string 1. \n");
}
Testing
str_conteudo ("Frase de teste","de"); //6
str_conteudo ("Frase de teste","teste"); //9
str_conteudo ("Frase de teste","Frase"); //0
str_conteudo ("Frase de teste","cuFra"); // não contem
str_conteudo ("Frase de teste","testem"); // não contem
See these tests in Ideone
The biggest difference to the code you had is in what was added inside the if
in the while
.
It’s never a good idea to write or read content. They should only return the result, giving freedom to those who call to do what they want with this result. Besides if
that has in the while
nor would it be necessary to change a few more things.
Taking these points that I mentioned I was switching function to something like:
int str_conteudo( char str1[], char str2[]) {
int i=0, j, k;
for(i = 0; str1[i] != '\0'; ++i) {
for (j = i,k = 0;str1[j]!= '\0' && str2[k] != '\0' && str1[j] == str2[k]; ++j,k++);
if (str2[k] == '\0'){
return i;
}
}
return -1;
}
Of course I could have switched the iteration and access to pointer syntax that would be shorter, but I chose to keep it with index to make the solution comparable and similar to yours.
Now the tests have to show the value obtained by the function, which returns -1
whenever you can’t find the substring:
printf("%d\n", str_conteudo ("Frase de teste","de")); //6
printf("%d\n", str_conteudo ("Frase de teste","teste")); //9
printf("%d\n", str_conteudo ("Frase de teste","Frase")); //0
printf("%d\n", str_conteudo ("Frase de teste","cuFra")); //-1
printf("%d\n", str_conteudo ("Frase de teste","testem")); //-1
See these tests also on Ideone
Just for reference, I leave the same example of top implemented with pointer notation, although exactly the same solution:
int str_conteudo( char *str1, char *str2) {
char *curr1 = str1, *s1, *s2;
while(*curr1){
for(s1 = curr1, s2 = str2; *s1 && *s2 && *s1 == *s2; ++s1, ++s2);
if (!*s2) return curr1 - str1;
curr1++;
}
return -1;
}
As a final note, the logic you are implementing is that the native function strstr
provides.
The problem you’re trying to solve is called
substring
. Searching for substring search algorithms and understanding them in depth will help you a lot– Jefferson Quesado