Wednesday, 10 April 2013

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



Program that displays the position or index in the string S where the string T begins , or -1 if S doesn’t contain T

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 step 5
Step 5: print the -1
Step 6: stop



#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();         
}

No comments: