C Program to Find Substring or Subsequence Within a String

Are you looking to learn the concept of “C Program to Find Substring or Subsequence Within a String“? If yes then you are in the perfect article. Let’s move on to the article .

C Program to Find Substring or Subsequence Within a String

C Program to Find Substring or Subsequence Within a String

Program Code

#include <stdio.h>

int main()
{
    char str1[80], str2[80];
    int l, i, j;

    printf("Enter first string: ");
    gets(str1);

    printf("Enter second string: ");
    gets(str2);

    //finding length of second string
    for (l = 0; str2[l] != ''; l++);

    for (i = 0, j = 0; str1[i] != '' && str2[j] != ''; i++)
    {
        if (str1[i] == str2[j])
        {
            j++;
        }
        else
        {
            j = 0;
        }
    }

    if (j == l)
    {
        printf("Substring found at position %d", i - j + 1);
    }
    else
    {
        printf("Substring not found");
    }

    return 0;
}

Final Thoughts

We hope this article about “C Program to Find Substring or Subsequence Within a String” will be beneficial to you. If you have any doubts then ask us via the comment section. Sharing is encouraging. Share this article with others to help them.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment