Q. Write a program which will read a paragraph and count all occurrences of a particular word specified by a user.
#BIT-FirstSem-2018-GroupB-9No-8Marks-Solution
==> Code goes like this:
#include <stdio.h>
#include <string.h>
char str[100], sub[100];
int count = 0, count1 = 0;
void main()
{
int i, j, l, l1, l2;
printf("\nEnter a string : ");
scanf("%[^\n]s", str);
l1 = strlen(str);
printf("\nEnter a substring : ");
scanf(" %[^\n]s", sub);
l2 = strlen(sub);
for (i = 0; i < l1;)
{
j = 0;
count = 0;
while ((str[i] == sub[j]))
{
count++;
i++;
j++;
}
if (count == l2)
{
count1++;
count = 0;
}
else
i++;
}
printf("%s occurs %d times \n", sub, count1);
}
Output:
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Read a paragraph and count all occurances of a particular word specified by a user - C
10:34 AM
C Programs
,
Occurrence of particular word in a paragraph string
,
OldQuestionSoluton2018C
,
Strings in c
Edit
0 comments:
Post a Comment