This is a C Program to find the frequency of substring in the given string.
This program finds the frequency of substring in the given string.
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.
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.
/*
* C Program to Find the Frequency of Substring in
* the given String
*/
#include <stdio.h>
#include <string.h>
void main()
{
int count = 0, i, j = 0, k;
char str[100], str1[20];
printf("Enter the string:\n");
scanf(" %[^\n]s", str);
printf("Enter the substring to be matched:\n");
scanf(" %[^\n]s", str1);
k = strlen(str1);
for (i = 0; str[i] != '\0'; i++)
{
while (str[i] == str[j])
{
j ++;
}
if (j == k)
{
count ++;
j = 0;
}
}
printf("No of matches of substring in main string is: %d\n", count);
}
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.
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
- Check Computer Science Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Check C Books
- Practice BCA MCQs