Read a paragraph and count all occurances of a particular word specified by a user - C

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   




Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment