C Program to check if a sub string is present in the string


February 9, 2022, Learn eTutorial
1226

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to search a substring in a string?

This c program is for checking if a string that is present in the main string or not. So we need to search in the main string for the occurrence of the substring. First, we have to accept the main string. Save the substring which the user wants to search.

Now check whether the substring is present in the main string or not. If the searching string is present, display the search is successful; otherwise, display search is unsuccessful.

How to search for substring implemented in C?

To implement the logic in the c program, first declare two character arrays to store the main string and the searching string. Then we will initialize two integer variables to keep the index of the character arrays as 'count1' and 'count2'.Then we will accept the main string as 'str' and the searching string as 'search'.

By using a 'while loop' with the condition 'str[count1]!=0' increment 'count1' by 1. When we exit from the loop, the variable 'count1' will contain the length of the main string. Using another while loop with the condition ' search [count2]!=0', increment count2 by 1. So the 'count2' contains the length of the searching string.

Using the nested for loop, we check the searching string in the main string or not by setting the flag variable. If the 'flag=1', then we will display the searching successfully. Otherwise, we will display the searching is unsuccessful.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the Array 'str[80]', 'search[10]' to store the String and searching String.

STEP 3: Declare some integer variables 'count1=0, count2=0, I, j, flag'.

STEP 4: Read the String from the user and store it into the variable 'str'

STEP 5: Read the Substring in which the user wants to Search for the Variable Search.

STEP 6: By using a while loop with the condition 'str[count1]!=0', increment 'count1' by 1 to get the length of the first String.

STEP 7: Using another 'while loop' with the condition ' Search [count2]!=0' then increment 'count2'  by 1, to get the length of the searching substring.

STEP 8: Using a For loop with the condition 'i <=count1-count2' do step 9.

STEP 9: By using another for loop set 'j=1'.

STEP 10: Check the condition 'j'.

STEP 11: If 'str[j]!=search[j-1]' then set Flag=0 and exit from the loop.Otherwise increment 'j' by 1 and repeat step 10.

STEP 12: Check if 'Flag==1' then goto step 14.

STEP 13:  Increment 'i' by 1 and repeat step 8.

STEP 14: If 'flag=1' then Display Search successfull else Display Search unsuccessful using puts() function.

C Source Code

                                          #include<stdio.h>
#include<string.h>

void main() {
  char str[80], search[10];
  int count1 = 0, count2 = 0, i, j, flag; /* declares the array and variables */
  puts("Enter a string:");
  gets(str);
  puts("Enter search substring:"); /* user enter the string and sub string */
  gets(search);
  while (str[count1] != '\0')
    count1++;
  while (search[count2] != '\0') /* calculating the length of both strings */
    count2++;
  for (i = 0; i <= count1 - count2; i++) {
    for (j = i; j < i + count2; j++) {
      flag = 1;
      if (str[j] != search[j - i]) /* checking each element of the sub string with main string to check for the occurrence of the sub sting */ {
        flag = 0;
        break;
      }
    }
    if (flag == 1) /* prints the result as successful if the flag is one else not successful */
      break;
  }
  if (flag == 1)
    puts("SEARCH SUCCESSFUL!");
  else
    puts("SEARCH UNSUCCESSFUL!");
  getchar();
}
                                      

OUTPUT

Enter a string:
Hello how are you?

Enter search substring:
how

SEARCH SUCCESSFUL!