C Program to Count the Occurrence of a Substring in String

This is a C Program to count the occurrence of a substring in string.

Problem Description

This program takes a string and a substring as input and counts the occurrence of a substring in string.

Problem Solution

1. Take a string and a substring as input.
2. Firstly check for the substring in the string.
3. If it is present then count the number of times it is present.

Program/Source Code

Here is source code of the C Program to count occurrence of a substring in string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program To Count the Occurrence of a Substring in String 
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. char str[100], sub[100];
  8. int count = 0, count1 = 0;
  9.  
  10. void main()
  11. {
  12.     int i, j, l, l1, l2;
  13.  
  14.     printf("\nEnter a string : ");
  15.     scanf("%[^\n]s", str);
  16.  
  17.     l1 = strlen(str);
  18.  
  19.     printf("\nEnter a substring : ");
  20.     scanf(" %[^\n]s", sub);
  21.  
  22.     l2 = strlen(sub);
  23.  
  24.     for (i = 0; i < l1;)
  25.     {
  26.         j = 0;
  27.         count = 0;
  28.         while ((str[i] == sub[j]))
  29.         {
  30.             count++;
  31.             i++;
  32.             j++;
  33.         }
  34.         if (count == l2)
  35.         {
  36.             count1++;                                   
  37.             count = 0;
  38.         }
  39.         else
  40.             i++;
  41.     }    
  42.     printf("%s occurs %d times in %s", sub, count1, str);
  43. }
Program Explanation

1. Take a string and a substring as input and store it in the array str and sub respectively.
2. Find the length of both the strings using strlen function.
3. Using for loop find whether the substring is present or not. If it is present then count the number of times it is present using the variable count.
4. Print the variable count as output.

advertisement
Runtime Test Cases
 
 
Enter a string : prrrogram c prrrogramming
 
Enter a substring : rr
rr occurs 2 times in prrrogram c prrrogramming

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Free 30-Day Python Certification Bootcamp is Live. Join Now!
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.