Q. Write a C program to accept two string from user, first is main string and second is searching sub string from main string, and count how many times its repeat individual in main string.
For example:
First main string: This is a good program
Second search string: is
Result: 1
Ans.
/*c program for search sub string from main string and counting how many times its repeat individual*/
#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-1]==' ')
if(str[i]==fstr[0])
{
for(j=0; str[i]==fstr[j] && fstr[j]!='\0'; i++,j++);
i--;
if(fstr[j]=='\0')
count++;
}
}
printf("%d",count);
getch();
return 0;
}
/***************Output***************/
Related Programs:
OR
Q. WAP to accept two string and find out how many times individual sub-string found in main string.For example:
First main string: This is a good program
Second search string: is
Result: 1
Ans.
/*c program for search sub string from main string and counting how many times its repeat individual*/
#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-1]==' ')
if(str[i]==fstr[0])
{
for(j=0; str[i]==fstr[j] && fstr[j]!='\0'; i++,j++);
i--;
if(fstr[j]=='\0')
count++;
}
}
printf("%d",count);
getch();
return 0;
}
/***************Output***************/
Screen shot for search sub-string individual from main string |
Related Programs:
Comments
Post a Comment