Write a C program that allows the user to enter two strings s1 and s2 and checks if s2 is a subtring of s1. If this is the case, it reports the index of s1 at which an instance of s2 is present, otherwise it reports that the substring was not found. For example, cat and nate are two subtrings of concatenate. (The only library functions you are allowed to use are printf and fgets). Your program must print appropriate prompts and messages.
Here is my code so far:
Code:
#include <stdio.h>
int main()
{
char s1[6];
char s2[6];
char temp[255];
int i,j,k=1;
printf("\nEnter first string1 (word 5 characters or less): ");
fgets(s1, 6, stdin);
gets(temp);
printf("\nEnter substring to find in string1 (word 5 characters or less): ");
fgets(s2, 6, stdin);
for(i=0; i<6; i++)
{
for(j=0; j<6; j++)
{
if(s2[i]==s1[j])
{
//printf("%c%c\n", s2[i], s1[j]);
//break;
}
}
}
return(0);
}
Right now, I have it so that the user inputs a 5 word character for s1 and s2. I have the for loop as a setup to check if s2 has the same characters as s1. The problem I am having is trying to report that the index of s1 at which an instance of s2 is present, otherwise it not being found...
I was thinking about trying to use a way to use true and false flags so that if s2 is true (meaning it is present in s1), then it would print a message. If s2 is false (meaning s2 is not present in s1), then it would say substring was not found. I don't understand how to implement it...
What would be the best way?
Can anyone help me out??