C Program to Check if the Substring is Present in the Given String

This is a C program to check if the substring is present in the given string.

Problem Description

This C Program checks the substring is present in the given string.

Problem Solution

The program accepts both string and substring. Then checks whether the substring is present in the given string.

Program/Source Code

Here is source code of the C program to check the substring is present in the given string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*  
 * C program to accept a string and a substring and
 * check if the substring is present in the given string
 */
#include<stdio.h>
 
int main()
{
    char str[80], search[10];
    int count1 = 0, count2 = 0, i, j, flag;
 
    printf("Enter a string:");
    gets(str);
    printf("Enter search substring:");
    gets(search);
    while (str[count1] != '\0')
        count1++;
    while (search[count2] != '\0')
        count2++;
    for (i = 0; i <= count1 - count2; i++)
    {
        for (j = i; j < i + count2; j++)
        {
            flag = 1;
            if (str[j] != search[j - i])
            {
                flag = 0;
                break;
            }
        }
        if (flag == 1)
            break;
    }
    if (flag == 1)
        printf("SEARCH SUCCESSFUL!");
    else
        printf("SEARCH UNSUCCESSFUL!");
 
    return 0;
}
Program Explanation

In this C program, we are reading a string using gets() function ‘str’ character variable. We are reading value another string to search using search character variable. To check substring is present in the given string. While loop is used to compute the str[] and search[] array variable value is not equal to null.

advertisement
advertisement

If the condition is true then execute the iteration of the loop. Increment the values of ‘count1 and count2 variable values. Now we are using two for loops to check if the substring is present in the given string. We are initializing the ‘i’ variable value to 0 and the loop will execute till the condition that ‘i’ variable value should be less than or equal to the difference of count1 and count2 variable values.

If the condition is true, then another for loop will execute by initializing the ‘i’ variable value to ‘j’ variable. And the loop will terminate if the ‘j’ variable value is less than the sum of ‘i’ variable value with count2 variable value if the condition is true.

Then it will execute the loop by assigning the flag variable value as 1 and if condition statement is used to check the str[] array variable value is not equal to search[] with the base index is the difference between ‘j’ variable and ‘i’ variable value. If the condition is true then it will execute the statement and assign flag variable value as 0.
For loop iteration will terminate till the condition becomes false. If-else condition statement is used to check if flag variable value is equal to 1 then print as search successful otherwise if the condition is false then print the statement as search unsuccessful.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
$ cc pgm44.c
$ a.out
Enter a string: hello
Enter search substring: world
SEARCH UNSUCCESSFUL!
 
$ a.out
Enter a string: helloworld
Enter search substring:ld
SEARCH SUCCESSFUL!

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.