C Program to Find the Frequency of a Substring in a String

«
»

This is a C Program to find the frequency of substring in the given string.

Problem Description

This program finds the frequency of substring in the given string.

Problem Solution

1. Take a string and a substring as input.
2. Compare the substring with the main string.
3. Count the number of times it matches in the main string and print the count as output.

advertisement
Program/Source Code

Here is source code of the C Program to find the frequency of substring in the given 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 Find the Frequency of Substring in 
  3.  * the given String
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void main()
  9. {
  10.     int count = 0, i, j = 0, k;
  11.     char str[100], str1[20];
  12.  
  13.     printf("Enter the string:\n");
  14.     scanf(" %[^\n]s", str);
  15.  
  16.     printf("Enter the substring to be matched:\n");
  17.     scanf(" %[^\n]s", str1);
  18.  
  19.     k = strlen(str1);
  20.  
  21.     for (i = 0; str[i] != '\0'; i++)
  22.     {
  23.         while (str[i] == str[j])
  24.         {
  25.             j ++;
  26.         }
  27.  
  28.         if (j == k)
  29.         {
  30.             count ++;
  31.             j = 0;
  32.         }
  33.     } 
  34.     printf("No of matches of substring in main string is: %d\n", count);
  35. }
Program Explanation

1. Take a string and a substring as input and store it in the arrays str[] and str1[] respectively.
2. Using for loop compare str1[] with the str[].
3. Do step-2 until the end of the main string.
4. During the comparison increment the variable count whenever the substring matches in the main string.
5. Print the variable count as output.

advertisement
advertisement
Runtime Test Cases
Enter the string:
prrrogram is prrrogramming
Enter the substring to be matched:
rr
No of matches of substring in main string is: 4
 
Enter the string:
Sanfoundry C Programming
Enter the substring to be matched:
oun
No of matches of substring in main string is: 1

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

advertisement
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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.