Find All Substrings 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[] = "abc";
    int n = strlen(str);

    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            for (int k = i; k <= j; k++)
                printf("%c", str[k]);
            printf("\n");
        }
    }
    return 0;
}

C Output

Input:  
abc  

Output:  
a
ab
abc
b
bc
c


C++ Program

#include <iostream>
using namespace std;

int main() {
    string str = "abcd";
    int n = str.size();

    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            cout << str.substr(i, j - i + 1) << endl;
        }
    }
    return 0;
}

C++ Output

Input:  
abcd 

Output:  
a
ab
abc
abcd
b
bc
bcd
c
cd
d


JAVA Program

public class Substrings {
    public static void main(String[] args) {
        String str = "xyz";
        int n = str.length();

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j <= n; j++) {
                System.out.println(str.substring(i, j));
            }
        }
    }
}

JAVA Output

Input:  
xyz 

Output:  
x
xy
xyz
y
yz
z


Python Program

s = "pqrs"
n = len(s)

for i in range(n):
    for j in range(i+1, n+1):
        print(s[i:j])

Python Output

Input:  
pqrs  

Output:  
p
pq
pqr
pqrs
q
qr
qrs
r
rs
s


In-Depth Explanation
Example
If the string is "abc", then the substrings are:
a, ab, abc, b, bc, c. Observe that substrings are always contiguous (continuous components of the string). That is, "ac" is not a substring, it's a subsequence. That makes a big difference in programming and interviews.

Real-Life Analogy
Consider a word that is on paper. If you cut the word from letter to letter with a pair of scissors without skipping the letters in between, then you have a substring. But if you cut letters randomly and skip some, you have a subsequence. For instance, "program" has substrings such as "pro", "gram", "rog", but "pgm" isn't a substring, it's a subsequence.

Why It Matters
Substrings are the building blocks of string processing. They are extensively applied in search engines, text editors, DNA sequence analysis, and cryptography. For instance, when you search for "play" within "display", the algorithm checks substrings of "display".

Computing all substrings is also the initial step towards solving problems such as finding longest palindromic substring, smallest unique substring, or verifying string patterns. 

Learning Insights
Total substrings of a string of size n is n*(n+1)/2. For the "abc", size 3, we have 3*4/2 = 6 strings.

Substrings are contiguous pieces of the original string whereas subsequences can skip characters.

Time complexity is O(n²) since for every start point we create several substrings.

Interview Use Case
Substring questions are typically asked to examine nested loop programming, string cutting, and time complexity skills. Examples include "Find the longest substring without repeating characters" or "Determine if a string is a substring of another." Having clarity on substrings vs subsequences can keep you from confusion during challenging coding interviews.

Real-World Application
In bioinformatics, sequence analysis of DNA and proteins employs substring searching to locate mutations or repeated motifs. In cybersecurity, substring detection is employed in pattern matching algorithms to locate malicious code snippets embedded deep within large files. In text processing, substrings are employed for autocomplete and spell-check features.

SEO-Friendly Conclusion
Locating all substrings of a string is a basic programming task that trains nested loops, string slicing, and uninterrupted sequence management. Beginners tend to mix up substrings with subsequences, but substrings are always consecutive. String manipulation using substring logic aids in the solution of complex problems such as longest palindrome substring, pattern searching, and text search algorithms. Whether studying for coding interviews, competitive programming, or actual applications such as bioinformatics and search engines, substring generation is an essential skill that creates a solid foundation in string manipulation. With this step-by-step tutorial along with C, C++, Java, and Python code examples, it will be simpler for beginners as well as students to understand the concept thoroughly and implement it in real-world coding issues.