Search This Blog

Saturday, 1 January 2022

Title:- Progam to count the occurrence of a substring in the string

 Title:- Progam to count the occurrence of a substring in the string


Program

/* C Program To Count the Occurrence of a Substring in String  */
#include <stdio.h>
#include<conio.h>
#include <string.h>
 
char str[100], sub[100];
int count = 0count1 = 0;
 
int main()
{
    int ijll1l2;
    
    printf("\nEnter a string : ");
    scanf("%[^\n]s"str);
 
    l1 = strlen(str);
 
    printf("\nEnter a substring : ");
    scanf(" %[^\n]s"sub);
 
    l2 = strlen(sub);
 
    for (i = 0i < 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 in %s"subcount1str);
    getch();
}


No comments:

Post a Comment

Search This Blog

Contact Form

Name

Email *

Message *

Popular Posts