×

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within strings. Next, we'll look at how to find the number of occurrences of a substring in a string. The C string library () provides a function (strstr()) that checks for the existence of substrings. But I don't know how often the substring is inside the string. Implement your function with or without the strstr() function.

Now we provide a few examples of the occurrence of a substring in C and the use of the function. Here is the solution:

  1. At first, it takes strings and substrings as input.
  2. Then, first look for a substring within the string.
  3. After that, count the instances that exist, if any. 

The following is the supply code for a C program that counts the wide variety of occurrences of a substring in a string. The C software builds and runs quality on my Linux gadget. Below is also the output of the utility.

Example 1: Here, we give an example of finding the occurrence of substring in C using function. The example is given below –

# include < stdio.h >  
# include < string.h >  
# include < stdlib.h >  
# include < conio.h >  
int substring_count (char * string, char * substring) 
{  
int i, j, S1, S2;  
int count = 0;  
 S1 = strlen (string);  
S2 = strlen (substring);  
for ( i = 0 ; i < S1 - S2 + 1 ; i + + ) 
{  
if (strstr( string + i , substring ) = = string + i ) 
{  
count + +;  
i = i + S2 - 1;  
}  
}  
return count;  
}  

Output: Now we compile the above program and also run it. After running this program, the output is given below –

Enter a string: If life were predictable, it would cease to be life and be without flavor.
Enter substring: he
Substring occurrence count is: 2

Explanation of the above program: Instead of creating my inner loop, I used the strstr() method to determine if a substring matched a specific position in the outer loop. A pointer to the first matching character is returned from the strstr() method. A counter is incremented when the pointer equals the outer loop start character pointer.

You can use the strcmp() method to achieve the same goal. The strrchr function returns the last character found in a string. Returns a NULL pointer instead of a character pointer if the character is not recognized.

Let's write a program that uses these functions to print each lowercase letter's first and last index in a string. Both strchr and strchr return a pointer to the character for which occurrences of the desired characteristics are recognized.

Let's say the character to search for is 'b', and the string to search for is 'abcd'. A pointer to the character "b" in the string is then returned. A null is returned if the surface is missing.

Example 2: Here, we give another example of finding the occurrence of substring in C using function. The example is given below –

# include < stdio.h >  
# include < string.h >  
# include < stdlib.h >  
# include < conio.h >  
char str [100], sub [100];  
int count = 0, count1 = 0;  
void main ()  
{  
 int i, j, l, l1, l2;  
 printf (" \ n Enter a string: ");  
 scanf (" % [^ \ n] s ", str );  
  l1 = strlen (str);  
 printf (" \ n Enter a substring: ");  
 scanf (" % [^ \ n] s ", sub);  
 l2 = strlen (sub);  
for ( i = 0 ; i < l1 ; i++ )  
 {  
j = 0;  
 count = 0;  
 while ((str [ i ] = = sub [ j ]))  
 {  
 count ++;  
  i ++;  
 j ++;  
  }  
if (count = = l2)  
{  
count1 ++;                                     
count = 0;  
}  
else  
 i ++;  
 }      
printf (" % s occurs % d times in % s ", sub, count1, str);
return 0;  
}  

Output: Now we compile the above program and also run it. After running this program, the result is given below –

Enter a string: progggram c progggramming
Enter a substring: gg
gg occurs two times in progggram c progggramming

Explanation of the above program: It takes strings and substrings as input and places them in the best str and sub fields. Use the strlen function to determine the length of each string. Use a for loop to determine if a substring exists. In this example, we use the matter variable to be counted the range of occurrences. Offers several variables as output.

 So, in this program, we briefly discuss how to find the occurrence of substring in C using function. We give some examples and share the output of these programs. 


Related Topics

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.