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:
- At first, it takes strings and substrings as input.
- Then, first look for a substring within the string.
- 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.