C Program to Count Substrings in a String

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

  1. Include the Standard Input-Output and String Libraries: Use #include <stdio.h> for standard input-output functions and #include <string.h> for string functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the main string, the substring, and the count of occurrences.
  4. Input the Strings: Use gets or scanf to take input from the user for both the main string and the substring.
  5. 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.
  6. Display the Count: Use printf to 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 mainStr is declared to store the main string, and subStr is declared to store the substring. The variables mainLen and subLen store the lengths of the main string and the substring, respectively. count is 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. The gets function is used to read the strings, allowing them to include spaces.

Step 3: Calculate the Lengths of the Strings

  • The strlen function from the string.h library is used to calculate the lengths of the main string and the substring.

Step 4: Count Substring Occurrences

  • The program uses a for loop 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 count is 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 printf function.

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.

Leave a Comment

Scroll to Top