Q. Write a C program to accept two string from user and find sub string from main string.
For example:
main string: This is a good program
sub string: is
result: 2
Ans.
/*c program to find how many times sub string found in main string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,count=0;
char str[40],fstr[10];
printf("Enter main string: ");
gets(str);
printf("Enter sub string: ");
gets(fstr);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]==fstr[0])
{
for(j=0; str[i]==fstr[j] && fstr[j]!='\0'; i++,j++);
if(fstr[j]=='\0')
count++;
}
}
printf("%d",count);
getch();
return 0;
}
/***************Output***************/
Related programs:
For example:
main string: This is a good program
sub string: is
result: 2
Ans.
/*c program to find how many times sub string found in main string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,count=0;
char str[40],fstr[10];
printf("Enter main string: ");
gets(str);
printf("Enter sub string: ");
gets(fstr);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]==fstr[0])
{
for(j=0; str[i]==fstr[j] && fstr[j]!='\0'; i++,j++);
if(fstr[j]=='\0')
count++;
}
}
printf("%d",count);
getch();
return 0;
}
/***************Output***************/
Screen shot of find sub-string in main string C program |
Related programs:
Comments
Post a Comment