0
I need to check if a string is contained in another and returns the position at which the first letter appeared.
I saw here on the site the following code, but I can not use the library string.h
.
include stdio.h
include string.h
int main()
{
char s1[20];
char s2[20];
printf("Digite uma string : ");
scanf("%s", s1);
printf("Digite outra string : ");
scanf("%s", s2);
printf("%ld", strstr(s1, s2) - s1);
}
I tried to do it this way:
void str_conteudo( char str1[], char str2[])
{
int i=0;
int y;
while(str1[i] == str2[i] && str2[i] != 0)
{
if (str2[i] != str1[i])
return i;
i++;
}
y=i;
printf("Indice do primeiro caractere que contem a string 2: %d ", y);
}
But it’s not increasing, which may be wrong?
Just a few notes: 1) if you select the code and click the button
{ }
(or useCtrl + K
), your code will be formatted; 2) [en.so] is not a forum.– Jéf Bueno
The problem itself is clear, but you’ve tried something?
– Jéf Bueno
@LINQ tried here but it didn’t work, I edit the code and put mine then?
– flavio
Yeah, you can [Dit] the question and just put down your attempt explaining what logic you used to get into the code and what was the problem you faced.
– Jéf Bueno
https://opensource.apple.com/source/tcl/tcl-10/tcl/compat/strstr.c.auto.html
– Victor Stafusa
@Is it normal to use that "pattern" that is being used in the parameters? I mean, to declare the types below and such. I’ve never seen that. I didn’t even know it was possible.
– Jéf Bueno
@LINQ This exists, is the original K&R form, but is considered archaic.
– Victor Stafusa