C Program Find Substring Index in String

By | March 12, 2020

C Program Find Substring Index in String

C program to input Source string
and a search substring to be searched
in first string. And displaying found or
not, using string function strstr().

#include <stdio.h>
#include <string.h>

int main()

	char str[100];
	char search[50];
	char *ptr;
	printf("Enter a string:\n");
	printf("Enter search substring:\n");
	ptr = strstr(str, search);

	if (ptr != NULL) /* Substring found */
		printf("'%s' contains '%s' on index %d\n", str, search, ptr-str);
	else /* Substring not found */
		printf("'%s' doesn't contain '%s'\n", str, search);

	return 0;


Enter a string:
easy code book
Enter search substring:
‘easy code book’ contains ‘code’ on index 5

 3,891 total views,  5 views today

Leave a Reply

Your email address will not be published.