C program to check if a substring exists in a string :
In this C programming tutorial, we will learn how to check if a substring exists in a string or not. If the substring exists, the position of this substring in the string should be printed. For example, for the string “Hello World!!”, substring World appears on position 7 of the main string. Similarly, if the substring doesn’t exist, print one failed message.The user will enter both of these inputs. Before going through the steps, let’s take a look at the program first. We are using a recursive approach in the below program :
C program: Recursive method to check if a substring exists in a string :
#include<string.h>
#include<stdio.h>
#include<ctype.h>
//12
int findSubString(char* mainString,char* subString,int mainLength,int subLength,int position,int subPosition){
//13
if(position >= mainLength || subPosition >= subLength){
return 1;
}
//14
if(isspace(subString[subPosition])){
return 1;
}
//15
if(mainString[position] == subString[subPosition]){
return findSubString(mainString,subString,mainLength,subLength,position + 1,subPosition + 1);
}
//16
return 0;
}
int main()
{
//1
char mainString[100];
char subString[100];
//2
int mainLength;
int subLength;
int i;
//3
int found = 0;
//4
printf("Enter main string : ");
fgets(mainString, 100, stdin);
//5
printf("Enter sub string : ");
fgets(subString, 100, stdin);
//6
mainLength = strlen(mainString);
subLength = strlen(subString);
//7
for(i = 0; i < mainLength - 1 ; i++){
//8
if(mainString[i] == subString[0]){
//9
if(findSubString(mainString,subString,mainLength,subLength,i,0)){
//10
printf("Input substring is found on position %d ",i+1);
found = 1;
break;
}
}
}
//11
if(found == 0){
printf("Input substring is not found in the string...");
}
}
Sample Output :
Enter main string : Hello World!!
Enter sub string : World
Input substring is found on position 7
Enter main string : This is a ball
Enter sub string : ball
Input substring is found on position 11
Enter main string : These are balls and bats
Enter sub string : balls
Input substring is found on position 11
Enter main string : This is a cat
Enter sub string : dog
Input substring is not found in the string...
Enter main string : Hello Everyone
Enter sub string : Hello
Input substring is found on position 1
Explanation :
The commented numbers in the above program denote the step number below :
- Create two char array mainString and subString to store the user input main and substrings.
- Create three integer variables. mainLength to store the length of the main string, subLength to store the length of the sub-string and _i _ to use in for-loop.
- Create one flag found and set the value to 0. 1 means sub-string is found in the main string and 0 means not found.
- Ask the user to enter the main string, read it and store it in array mainString.
- Similarly, read and store the sub-string in variable subString.
- Calculate the lengths of both of these strings and store it in mainLength and subLength variables.
- Start a for loop to read each character of the main string one by one.
- For each character, compare it with 0 position character of the sub-string.
- If both are equal, call findSubString function. This function recursively checks if all characters of the sub-string exist in the main string or not. It returns 1 if true.
- If the sub-string exists in the main string, print out its position and set the value of flag found as 1.
- If the substring is not found, print one message.
- findSubString(char* mainString,char* subString,int mainLength,int subLength,int position,int subPosition) is the main function to find sub-string in a string. It returns 1 if found. It calls itself again and again recursively to compare characters of both strings.
- Return 1 if the current position of any of the string is more than its length.
- Check if a character is space or empty by using isSpace() function. If current character of sub-string is empty, return 1.
- If current character of both of these strings is equal, call the same function recursively. Increment position for both strings by 1.
- Return 0 if nothing is true above.