INTERVIEW QUESTION C(ZOHO)
******************************************************************************QUESTION
Find if a String2 is substring of String1. If it is, return the index of the first occurrence. else return -1.Eg 1:Input:
String 1: test123string
String 2: 123
Output: 4
Eg 2: Input:
String 1: testing12
String 2: 1234
Output: -1
*******************************************************************************/
PROGRAM:
#include <stdio.h>#include<string.h>
int main()
{
char a[100],b[100];
int i,j,flag=0,n,k;
scanf("%s %s",a,b);
for(i=0;i<strlen(a);i++)
{
k=i;
for(j=0;j<strlen(b);j++)
{
if(a[k]!=b[j])
{
break;
}
if(a[k]==b[j] && j==(strlen(b)-1))
{
flag=1;
n=i+1;
break;
}
k++;
}
if(flag==1)
{
printf("%d",n);
break;
}
}
if(flag==0)
{
printf("-1");
}
}
Comments
Post a Comment