Program to Count Occurrences of substring in String in C Language

Program-to-Count-Occurrences-of-substring-in-String-in-C-Language

Program Description:

Write a Program to Count Occurrences of substring in String in C Programming language. The program should accept two strings from the user, one for the main string and one for the substring. The program needs to count all occurrences of the substring within the main string and return the result.

📢 This Program is part of the C String Practice Programs series

We are going to look at a couple of methods to find all occurrences of a substring in a string and each program will be followed by a step-by-step explanation and program output. We also provided instructions for compiling and running the program using your compiler.

Let’s look at the example input and output of the program.

Input and Output:

Input:

Enter a main string : git init, git add, and git commit

Enter the substring(String to search in main string): git

Output:

FOUND! Substring:'git' appeared 3 times in the main string

The substring git is appeared three times in the input string.

📢 Note: The program is case-sensitive. As a result, upper-case characters will differ from lower-case characters.

Prerequisites:

Please go through the C-Strings, C-Arrays, and Functions in C to understand the following program better.

We will look at two methods to count all occurrences of the substring in the string. They are

  • Manual Approach(Iterative)
  • User-defined function

Count All Occurrences of a substring in a string in C Program Explanation:

The instructions to find all occurrences of a string in a string using an iterative method.

  1. Start the main() function by declaring the two strings and let’s say the main string as mainStr and the substring as subStr. The size of both strings is equal to SIZE macro.
  2. Initialize a variable called counterwith the value Zero(0). The counter variable will be used to count the number of occurrences of the substring in the main string.
  3. Read the two strings from the user and store them in mainStr and subStr respectively. We use the gets function to read the strings.
  4. To Count number of occurrences of substring( subStr) in the main string( mainStr), Iterate through the main string( mainStr), looking for the first character of the substring( subStr[0]). If we found the first character of the substring, we should then determine whether all other characters of the substring match. If that’s the case, we found the substring( subStr) in the main string( mainStr). Similar way we need to look for all occurrences of the substring.
  5. Create a For loop by initializing variable i with 0 to iterate over the mainStr. It will continue looping as long as the current character ( mainStr[i]) is not a NULL character ( '\0').
    • At each iteration, Check if the current character( mainStr[i]) is equal to the first character of the substring( subStr[0]) - i.e if(mainStr[i] == subStr[0])
    • If the above condition is true, Then search for all characters of the subStr in the mainStr. We need to use another for loop to iterate over the contents of the subStr – i.e for(j=0; subStr[j]; j++).
      • Check if the mainStr[k] (where k = i) is not matching to subStr[j], If so then the subString is not found. Otherwise, continue to check all elements of the subStr.
    • Once the above loop is completed, Check if we iterated complete subStr ( use j == strlen(substr) ). If we iterated the complete substring( subStr), then we found the substring in the main string. So increment the counter variable – counter++;
    • Continue to the next iteration to search for remaining occurrences of the substring ( subStr).
  6. If step 6 is completed, The counter variable contains the total number of times the substring appeared in the main string.
    • If counter is equal to zero(0), Then the substring( str) is not found the main string( mainStr). Display the NOT FOUND message on the console.
    • If counter contains a value greater than zero, Then we found the substring. Display the FOUNDmessage along with the number of appearances.
  7. Stop the program.

Program to Count All Occurrences of Substring in a sentence in C:

Here is the program count all occurrences of word in a sentence in c.

Program Output:

Let’s compile and Run the program. We are using the GCC compiler to Compile and Run the C programs.

Test Case 1: Successful case:

count-all-occurrencess-of-substring-in-string-in-c-program-output

As we can see from the above output, The substring git is found in the main string( git init, git add, and git commit). It appeared three times.

Test case 2: Negative Case:

The character/substring C is not found in the main string. So program displayed the NOT FOUND message.

C Program to Count Occurrences of substring in String using a user-defined function:

In this method, we will break the above program into functions. Let’s create  function to count the number of appearances of a substring in the main string.

We have defined a function called countSubStr, This function is used to count the number of times the substring has appeared in the main string.

int countSubStr(char *mainStr, char * subStr)

As we can see from the above prototype, This function takes two character pointers( char *) as formal arguments. The first argument is mainStr and the second argument is subStr. The countSubStr() function counts all occurrences of the subStr in the mainStr.

This function also returns an integer value, It returns the total number of times subStr is found in the mainStr. If subStr not found in the mainStr, Then it will return .

To count the number of occurrences of the substring in the main string, Call the countSubStr function from the main() function.

Check the return value(i.e cnt) of the countSubStr() function and display the results.

Program Output:

Let’s compile and run the program.

count-all-occurrencess-of-word-in-string-program

The substring is appeared four times in the main string.

As we can see from the above examples, The countSubStr() function is properly counting the number of occurrences of the substring in a string and returning the results.

Related String Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply