C/C++ Program to Find Substring in String (Pattern Matching)

Here you will get C and C++ program to find substring in string.

Pattern matching refers to find the position where a string pattern appears in a given string. If the required string is not present in given text, then it returns the value zero.

If the required string is present in a given string, it returns the position of occurrence of required string or substring.

C Program

#include<stdio.h>

int main()
{
    int i,j,temp;
    char str[100]={"This is a pattern matching"};
    char substr[20]={"pattern"};

    for(i=0;str[i]!='\0';i++)
    {
        j=0;
        if(str[i]==substr[j])
        {
            temp=i+1;
            while(str[i]==substr[j])
            {
                i++;
                j++;
            }

            if(substr[j]=='\0')
            {
                printf("The substring is present in given string at position %d\n",temp);
                exit(0);
            }
            else
            {
                i=temp;
                temp=0;
            }
        }
    }

    if(temp==0)
        printf("The substring is not present in given string\n");

    return 0;
}

C++ Program

#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
    int i,j,temp;
    char str[100]={"This is a pattern matching"};
    char substr[20]={"pattern"};

    for(i=0;str[i]!='\0';i++)
    {
        j=0;
        if(str[i]==substr[j])
        {
            temp=i+1;
            while(str[i]==substr[j])
            {
                i++;
                j++;
            }

            if(substr[j]=='\0')
            {
                cout<<"The substring is present in given string at position "<<temp<<"\n";
                exit(0);
            }
            else
            {
                i=temp;
                temp=0;
            }
        }
    }

    if(temp==0)
        cout<<"The substring is not present in given string\n";

    return 0;
}
Output
C/C++ Program to Find Substring in String (Pattern Matching)

9 thoughts on “C/C++ Program to Find Substring in String (Pattern Matching)”

  1. sorry but this algorithm does not work,take for an example last word “matching” it should write number 18 or 17 but it say 1. 🙂

  2. Abhishek Kumar Singh

    If i give “abhisheshek” in the first string and “shek” in the substring then the a both of the above codes show “the substring is not present in the given string”, but it is present.
    Please correct it for me without using strstr function.

  3. In while loop you have to give 3 condition
    While(str[i]!=’\0′ &&substr[j]!=’\0′ &&str[i]==substr[j])
    That’s it programme will work fine

  4. Hi guys, here is the fixed code :

    #include
    #include
    using namespace std;

    int main(){

    int i,j,temp;
    string s1,s2;

    cout<<"Enter the string :";
    getline(cin,s1);
    cout<<"Enter the substring :";
    getline(cin,s2);

    for(i=0;i<s1.length();i++){
    j=0;

    if(s1[i]==s2[j]){
    temp=i;
    while(s1[i]!='\0' && s2[j]!='\0' && s1[i]==s2[j]){
    i++;
    j++;
    }

    if(s2[j]=='\0'){
    cout<<"\nThe substring exists in the given string!";

    }

    else{
    i=temp;
    temp=0;
    }

    }
    }

    if(temp==0)
    cout<<"\nThe substring doesn't exist in the given string!";
    }

Leave a Comment

Your email address will not be published. Required fields are marked *