I am writing a simple code to find a string s1 from a string s2 and then saving it in string s3. The function returns 1 if the string s1 is present in s2 else 0. I am using pointers as parameters. The problem is that even when I am assigning s1 to n, line 30 of code isn't providing me correct output. Also, the code is behaving abnormally. Can you please point out the error?
#include<string.h>
#include<stdio.h>
int code(char * s2, char * s1, char * s3)
{
char h[] = "";
strcpy(h, s2);
printf("%s\n", h);
char n[] = "";
strcpy(n, s1);
printf("%s\n", n);
char b[] = "";
int i = 0, j = 0, flag = 0, flag2 = 0;
for (i = 0; n[i] != '\0'; i++)
{
printf("%d\n", i);
printf("%c\n", n[i]);
for (j = 0; h[j] != '\0'; j++)
{
printf("%c\n", h[j]);
if (n[i] == h[j])
{
flag = 1;
break;
}
}
if (flag == 1)
{
break;
}
}
printf("%d\n", i);
printf("%d\n", j);
printf("%c\n", n[i]);
printf("%c\n", h[j]);
printf("\n");
if (flag == 1)
{
printf("Here:1\n");
printf("%c\n", h[j]);
while (n[i] != '\0')
{
printf("Here: %d\n", i);
printf("%c\n", n[i]);
printf("%c\n", h[j]);
if (n[i] == h[j])
{
printf("Here:2 %d %d\n", i, j);
printf("%c\n", n[i]);
printf("%c\n", h[j]);
b[i + 1] = n[i];
i = i + 1;
j = j + 1;
}
else
{
printf("Here:3\n");
flag2 = 1;
break;
}
}
if (flag2 == 1)
{
printf("Here:4\n");
return 0;
}
else
{
printf("Here:5\n");
b[i + 1] = '\0';
s3 = b;
return 1;
}
}
else
{
printf("Here:6\n");
return 0;
}
}
Input: s2: vanilla s1: lla
Output: 1