- Take as input a string and a substring.
- Look first in the string for the substring.
- Count the instances where it is present if it is there.
Here is the source code in C for determining the number of times a substring appears within a string. This C program has been compiled and run without errors on a Linux system. Below, you can find the generated results of running this program.
Example:
/*
* C Program To Count the Occurrence of a Substring in String
*/
# include < stdio . h >
# include < string . h >
# include < stdlib >
# 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 ; )
{
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 ) ;
}
Output:
enter a string : prrrogram c prrrogramming
enter a substring : rr
rr occurs 2 times in prrrogram c prrrogramming
..............................................
Process executed in 2.22 seconds
Press any key to continue.
Explanation
- Take a string and a substring as input and place them in the corresponding str and sub fields.
- Use the strlen function to determine the length of both strings.
- Determine if a substring is present or not using a for loop. If so, use the count variable to count how many times it is present.
- Print the number of variables as output.
Using Function:
/*
* C Program To Count the Occurrence of a Substring in String
*/
# include < stdio . h >
# include < string . h >
# include < stdlib >
# include < conio . h >
int substring_count ( char * string , char * substring ) {
int i , j , l1 , l2 ;
int count = 0 ;
l1 = strlen ( string ) ;
l2 = strlen ( substring ) ;
for ( i = 0 ; i < l1 - l2 + 1 ; i + + ) {
if ( strstr ( string + i , substring ) = = string + i ) {
count + + ;
i = i + l2 - 1 ;
}
}
return count ;
}
Output:
enter a string : If life were predictable it would cease to be life and be without flavor
enter substring : be
Substring occurrence count is : 2
..............................................
Process executed in 1.22 seconds
Press any key to continue.
Explanation
We opted to use the strstr function instead of crafting a custom inner loop to check for substring matches starting from a specific position in the outer loop. strstr returns a pointer to the first matching character. If this pointer matches the initial character's pointer in the outer loop, the counter is incremented. Achieving a similar outcome is possible with the strcmp function. Additionally, the strchr function locates the first occurrence of each character within a string.
The strrchr function retrieves the final occurrence of a specified character within a string. In cases where the character is not found, it returns a NULL pointer instead of a character pointer. Below is a program designed to display both the initial and concluding index of each lowercase character in a given string by leveraging these functions.
Both strchr and strrchr functions return a pointer to the position of the specified character within a string. For instance, if we are searching for the character "b" in the string "abcd," a pointer to the character "b" would be the output. If the character is not found, NULL will be returned.