Write a C program that displays the position or index in the string S where the string T begins, or – 1 if S doesn’t contain T.

Algorithm:

Step 1: Start 
Step 2: read the string and then displayed 
Step 3: read the string to be searched and then displayed 
Step 4: searching the string T in string S and then perform the following steps 
            i. found = strstr(S, T) 
            ii. if found print the second string is found in the first string at the position. If not goto step5      
Step 5: print the -1 
Step 6: Stop 

Program:

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
   char s[30], t[20];
   char *found;
   clrscr();
   puts("Enter the first string: ");
   gets(s);
   puts("Enter the string to be searched: ");
   gets(t);
   found = strstr(s, t);
   if(found)
   {
      printf("Second String is found in the First String at %d position.\n", found - s);
   }
   else
   {
      printf("-1");
   }
   getch();
}

Input & Output:

1.Enter the first string:
kali 
Enter the string to be searched:
li 
second string is found in the first string at 2 position 

2.Enter the first string:
nagaraju 
Enter the string to be searched:
raju 
second string is found in the first string at 4 position 

3.Enter the first string:
nagarjuna 
Enter the string to be searched:
ma
-1