×

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

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.