In: Computer Science
Implement a substring search function which searches a string for a given keyword. The function should return a pointer to the address of the first character of the substring within the original string, if it exists. If the substring is not found within the string, return NULL. If the input is found, print "Key found." otherwise print "Key not found." Your program’s input and output should match what is seen in the example run. • You may only use stdio.h. • Save your code as prob3.c.
Example Run
Enter string: this is a test string.
Enter key: is
Key found.
#include <stdio.h>
char *substring(char *string, char *key);
int main()
{
char str[100], key[100];
printf("Enter String: ");
gets(str);
printf("Enter Key: ");
gets(key);
char *addr = substring(str,key);
return 0;
}
char *substring(char *string, char *key)
{
int l,i,j;
for (l = 0; key[l] != '\0'; l++);
for (i = 0, j = 0; string[i] != '\0' && key[j] != '\0';
i++)
{
if (string[i] == key[j])
{
j++;
}
else
{
j = 0;
}
}
if (j == l)
{
printf("Key found.");
}
else
{
printf("Key not found.");
}
return &string[i-j];
}