In its implementation it has the logic that goes through the letters of the main phrase, but it lacked the logic that sees if each letter is equal to the letters of the second string.
Trying to make the most of your logic can do this:
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] != '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
') { // 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]!='int str_conteudo( char str1[], char str2[]) {
int i=0, j, k;
for(i = 0; str1[i] != '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
'; ++i) {
for (j = i,k = 0;str1[j]!= '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;
}
' && str2[k] != '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] != '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
') { // 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]!='int str_conteudo( char str1[], char str2[]) {
int i=0, j, k;
for(i = 0; str1[i] != '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
'; ++i) {
for (j = i,k = 0;str1[j]!= '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;
}
' && str2[k] != '%pre%' && str1[j] == str2[k]; ++j,k++);
if (str2[k] == '%pre%'){
return i;
}
}
return -1;
}
' && str2[k]!='%pre%' && str1[j] == str2[k];++j,k++);
if (str2[k] == '%pre%'){ //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");
}
' && str1[j] == str2[k]; ++j,k++);
if (str2[k] == '%pre%'){
return i;
}
}
return -1;
}
' && str2[k]!='%pre%' && str1[j] == str2[k];++j,k++);
if (str2[k] == '%pre%'){ //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
%pre%
View these tests on Ideone
The biggest difference to the code you had is in what was added within if
in while
.
It is 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. In addition, the% w / o% w / o% w / w / w / w / w / w / w / w /
Taking these points I mentioned I changed the function to something like:
%pre%
Of course I could have switched the iteration and access to syntax of pointers that would be shorter, but I opted to keep index to make the solution comparable and similar to yours.
Now the tests have to show the value obtained by the function, which returns if
whenever it does not find the substring:
%pre%
See these tests on Ideone
Just for reference, I leave the same example of implemented top with pointers notation, although exactly the same solution:
%pre%
As a final note, the logic you are implementing is that the native function while
provides .