Introduction
Counting the occurrences of a substring within a string is a common problem in string processing. This guide will show you how to write a C program to count how many times a given substring appears in a string.
Problem Statement
Create a C program that:
- Takes a main string and a substring as input from the user.
- Counts the number of times the substring appears in the main string.
- Displays the count of the substring.
Example:
- Input:
- Main String = "Hello, World! Hello, Universe!"
- Substring = "Hello"
- Output: Substring Count = 2
Solution Steps
- Include the Standard Input-Output and String Libraries: Use
#include <stdio.h>for standard input-output functions and#include <string.h>for string functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the main string, the substring, and the count of occurrences.
- Input the Strings: Use
getsorscanfto take input from the user for both the main string and the substring. - Count Substring Occurrences: Use a loop to search for the substring within the main string and increment the count each time the substring is found.
- Display the Count: Use
printfto display the number of times the substring appears in the main string.
C Program to Count Substrings in a String
#include <stdio.h>
#include <string.h>
int main() {
// Step 1: Declare variables to hold the main string, substring, and count
char mainStr[100], subStr[100];
int i, j, k, mainLen, subLen, count = 0;
// Step 2: Prompt the user to enter the main string
printf("Enter the main string: ");
gets(mainStr); // Using gets to read the string including spaces
// Step 3: Prompt the user to enter the substring
printf("Enter the substring: ");
gets(subStr); // Using gets to read the string including spaces
// Step 4: Calculate the lengths of the main string and substring
mainLen = strlen(mainStr);
subLen = strlen(subStr);
// Step 5: Count occurrences of the substring in the main string
for (i = 0; i <= mainLen - subLen; i++) {
k = i;
for (j = 0; j < subLen; j++) {
if (mainStr[k] != subStr[j]) {
break;
}
k++;
}
if (j == subLen) {
count++;
}
}
// Step 6: Display the count
printf("Substring count: %d\n", count);
return 0; // Step 7: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variable
mainStris declared to store the main string, andsubStris declared to store the substring. The variablesmainLenandsubLenstore the lengths of the main string and the substring, respectively.countis used to count the occurrences of the substring.
Step 2: Input the Strings
- The program prompts the user to enter the main string and the substring using
printf. Thegetsfunction is used to read the strings, allowing them to include spaces.
Step 3: Calculate the Lengths of the Strings
- The
strlenfunction from thestring.hlibrary is used to calculate the lengths of the main string and the substring.
Step 4: Count Substring Occurrences
- The program uses a
forloop to iterate through the main string:- For each position in the main string, a nested loop checks if the substring matches starting at that position.
- If a match is found, the
countis incremented.
Step 5: Display the Count
- After counting the occurrences, the program displays the number of times the substring appears in the main string using the
printffunction.
Step 6: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example 1:
Enter the main string: Hello, World! Hello, Universe!
Enter the substring: Hello
Substring count: 2
Example 2:
Enter the main string: abababa
Enter the substring: aba
Substring count: 3
Example 3:
Enter the main string: This is a test string
Enter the substring: test
Substring count: 1
Conclusion
This C program demonstrates how to count the occurrences of a substring within a string by iterating through the main string and checking for matches. It covers basic concepts such as string manipulation, loops, and nested loops, making it a useful example for beginners learning C programming.