PROBLEM 31:
Write a C program to find a sub-string in a string.
Sample Input: you just blew my mind
Substring to search: my
Sample Output: Substring found.
CODE:
#include<stdio.h>
main()
{
// Variable declaration//
char a[100];
char b[100];
int i=0,j=0,count=0,count1=0;
/*Input*/
printf("Enter string:");
gets(a);
printf("Enter substring to found:");
gets(b);
/* actual Working */
for(i=0;b[i]!='\0';i++)//here i count total substring words
{
count=count+1;
}
for(i=0;a[i]!='\0';i++)//here I compare string1 value with string2
{
if(a[i]==b[j])// if it match so increment in count1 will perform
{
count1++;
j++;
}
if(count1==count)//if substring words equal to count1 so loop will break
{
printf("Substring found");
break;
}
}
if(count1!=count)// if count1 not equal
{
printf("no Substring found");
}
return 0;
}
Program Output:
For more problems click here
No comments:
Post a Comment