Code:
#include <stdio.h>
#include <string.h>
void findtrack(char ip[4][80] , char substring[])
{
int i;
for (i=0 ; i<4 ; i++)
{
if (strstr(ip[i] , substring ) )
printf("Matching track is:%d full name:%s\n" ,i+1 , ip[i]);
}
}
int main() {
char tracks[4][80];
char find[40];
printf("enter first track:");
fgets(tracks[0] , 80 , stdin);
printf("enter second track:");
fgets(tracks[1] , 80 , stdin);
printf("enter third track:");
fgets(tracks[2] , 80 , stdin);
printf("enter fourth track:");
fgets(tracks[3] , 80 , stdin);
printf("enter a string to find track:");
fgets(find , 40 , stdin);
findtrack(tracks , find);
}
Test Case1 -
Input-
enter first track:hello
enter second track:animal
enter third trackayphone
enter fourth track:magic
enter a string to find track:ma
Output-Nothing (didn't find "ma" in fourth track )
Test Case2- Input-
enter first track:hello
enter second track:animals
enter third track:case
enter fourth track:sadf
enter a string to find track:ls
Output- Matching track is:2 full name:animals
Can you please tell me why there is a difference in output even though the string to be found is present in one of the tracks in both the cases?
When the string to be found contains the last alphabet of one of the tracks , the code works , otherwise it does not .
Please guide me