Find Substring in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

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

int main() {
    char str[100], sub[100];
    scanf("%s %s", str, sub);
    if (strstr(str, sub))
        printf("Yes, substring found");
    else
        printf("No, substring not found");
    return 0;
}

C Output

Input:  
helloworld world 

Output:  
Yes, substring found


C++ Program

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str, sub;
    cin >> str >> sub;
    if (str.find(sub) != string::npos)
        cout << "Yes, substring found";
    else
        cout << "No, substring not found";
    return 0;
}

C++ Output

Input:  
programming gram 

Output:  
Yes, substring found


JAVA Program

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        String sub = sc.next();
        if (str.contains(sub))
            System.out.println("Yes, substring found");
        else
            System.out.println("No, substring not found");
    }
}

JAVA Output

Input:  
developer love

Output:  
No, substring not found


Python Program

s, sub = input().split()
print("Yes, substring found" if sub in s else "No, substring not found")

Python Output

Input:
datascience data 
Output:  
Yes, substring found


In-Depth Explanation
Example
Suppose you are looking for the string "world" within the larger string "helloworld". The string "world" begins at position 6 (index 5 when counting from zero). If it's there, we accept it; if not, we don't. This is precisely what substring search is — verifying whether a smaller word or character set exists within a larger one.

Real-Life Analogy
Consider a book as the principal string and a quote as the substring. If you desire to find out if that precise quote is of the book, you go through the pages and search for it. If you see it, that is equivalent to substring detection. If not, then that quote never surfaced in that book. In everyday life, looking up a contact's name in your phone is the same concept too — your entire phonebook is one long string, and the input for search is the substring.

Why It Matters
Substring finding is perhaps the most significant task in computer science due to how much programming involves text. Search engines seek words on web pages, compilers search source code for keywords, and mobile applications search logs, chat conversations, or emails. Whenever you type Ctrl+F in a document, you are executing a substring search in the background. Studying this little idea teaches you the premise of pattern matching.

Learning Insights
At the beginner's level, this issue teaches you to comprehend string handling functions such as strstr in C, find in C++, contains in Java, and the in keyword in Python. You will then later know that substring searching efficiently is a grand domain of computer science study with algorithms such as KMP (Knuth-Morris-Pratt), Rabin-Karp, and Boyer-Moore, which speed up search for enormous sets of data. So this small exercise is the seed that grows into efficient searching techniques.

Real-World Application
From spam email detection (looking for certain suspect words) to searching for DNA sequences within a genome string, substring search pervades the world. Even plagiarism detectors operate by examining if certain phrases within one document occur in another. In software interviews, substring questions try out both your fundamental grasp and sometimes compel you to optimize the code.

SEO-Optimized Closing
Knowing how to search for a substring in C, C++, Java, and Python is an essential programming skill each starter needs to understand. Substring search educates on the basics of string processing, text searching, and pattern matching, which are vital in practical applications such as search engines, compilers, and data analysis. Practice substring problems to enhance your string-handling skill, prepare for coding interview challenges, and solidify the foundation for advanced algorithms. Regardless of whether you study C, C++, Java, or Python, substring problems are one of the most frequent and handy programming exercises.