Program to check given Substring is present in a string - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Monday, January 27, 2020

Program to check given Substring is present in a string - My CS Tutorial

C program to check Substring with a string.
By this problem we will check whether a given substring is present in a given string.
In this post we will discuss that how to write a c program to check or find
whether a substring is present or available in a string.

In the first user will enter a string after it enter a substring then the compiler check that the given substring is available in string or not and print the statement.

Code


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int flag=0,len1,len2,a,b;
char st[100],sub[100];
clrscr();

printf("\n ENTER A STRING OR SENTENCE:\n ");
gets(st);

printf("\n ENTER A WORD OR SUBSTRING TO BE SEARCHED:\n ");

gets(sub);
len1=strlen(st);
len2=strlen(sub);

for(a=0;a<=len1-len2;a++)
{
    for(b=a;b<a+len2;b++)
    {
        flag=1;
        if(st[b]!=sub[b-a])
        {
            flag=0;
            break;
        }
    }
    
    if(flag==1)
    break;
}

if(flag==1)
{
    printf("\n SEARCH WORD OR SUBSTRING IS AVAILABLE.\n ");
}

else
{
        printf("\n SEARCH WORD OR SUBSTRING IS NOT AVAILABLE.\n ");
}

getch();
}


OUTPUT:




Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment