/*
Name: Write a C program to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T. */
/*
Author: Parveen Malik
Date: 18/09/11 08:58
*/
#include<stdio.h>
#include<conio.h>
int main(void)
{
char str[10],tr[10];
int i,j,pos;
int flag=0;
int count=0;
printf("Enter the main string : ");
gets(str);
printf("Enter the substring : ");
gets(tr);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==str[0])
{
pos=i;
for(j=0;tr[j]!='\0';j++)
{
if(str[i]==tr[j])
count++;
i++;
}
flag=1;
break;
}
}
if(flag==1&&count==strlen(tr))
printf("\nPostion :%d, and T is a substring of S",pos+1);
else if(flag==1&&count<strlen(tr))
printf("\nPosition : %d, and T is not a substring of S",pos+1);
else
printf("\nIndex : -1");
getch();
}
OUTPUT:
0 comments:
Post a Comment