Program to Find Sub String from the Given String

Program to Find Sub String from the Given String

  • Write a program to Find Sub String from the Given String in C
  • Write a program to Find Sub String from the Given String in C++
  • Write a program to Find Sub String from the Given String in Python
  • Write a program to Find Sub String from the Given String in PHP
  • Write a program to Find Sub String from the Given String in Java
  • Write a program to Find Sub String from the Given String in Java Script
  • Write a program to Find Sub String from the Given String in C#

Explanation:

We can use comparison or string search methods to extract a substring from a given string. Finding out whether a specific substring is present in the main string and returning the substring’s beginning position—or just verifying its existence—is the aim.

Logic Explanation:

  1. Search for the Substring:
    • You can loop through the main string and compare parts of the string with the substring.
  2. Return the Starting Position:
    • If the substring is found, return the index of its first occurrence.
    • If the substring is not found, return an indicator such as -1 (common in many programming languages).
  3. Edge Case Considerations:
    • If the substring is empty, it should always be considered found at position 0.
    • If the substring length is greater than the main string length, it can’t exist.

Program to Find Sub String from the Given String

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

void findSubstring(char *str, char *sub) {
    char *pos = strstr(str, sub);
    if (pos != NULL) {
        printf("Substring found at position: %ld\n", pos - str + 1);
    } else {
        printf("Substring not found.\n");
    }
}

int main() {
    char str[100], sub[50];

    printf("Enter the main string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';

    printf("Enter the substring: ");
    fgets(sub, sizeof(sub), stdin);
    sub[strcspn(sub, "\n")] = '\0';

    findSubstring(str, sub);

    return 0;
}

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

void findSubstring(const string &str, const string &sub) {
    size_t pos = str.find(sub);
    if (pos != string::npos) {
        cout << "Substring found at position: " << pos + 1 << endl;
    } else {
        cout << "Substring not found." << endl;
    }
}

int main() {
    string str, sub;

    cout << "Enter the main string: ";
    getline(cin, str);

    cout << "Enter the substring: ";
    getline(cin, sub);

    findSubstring(str, sub);

    return 0;
}

def find_substring(main_string, sub_string):
    pos = main_string.find(sub_string)
    if pos != -1:
        print(f"Substring found at position: {pos + 1}")
    else:
        print("Substring not found.")

main_string = input("Enter the main string: ")
sub_string = input("Enter the substring: ")

find_substring(main_string, sub_string)

<?php
function findSubstring($mainString, $subString) {
    $pos = strpos($mainString, $subString);
    if ($pos !== false) {
        echo "Substring found at position: " . ($pos + 1) . "\n";
    } else {
        echo "Substring not found.\n";
    }
}

echo "Enter the main string: ";
$mainString = trim(fgets(STDIN));

echo "Enter the substring: ";
$subString = trim(fgets(STDIN));

findSubstring($mainString, $subString);
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the main string: ");
        String mainString = scanner.nextLine();

        System.out.print("Enter the substring: ");
        String subString = scanner.nextLine();

        int pos = mainString.indexOf(subString);
        if (pos != -1) {
            System.out.println("Substring found at position: " + (pos + 1));
        } else {
            System.out.println("Substring not found.");
        }
    }
}

function findSubstring(mainString, subString) {
    const pos = mainString.indexOf(subString);
    if (pos !== -1) {
        console.log(`Substring found at position: ${pos + 1}`);
    } else {
        console.log("Substring not found.");
    }
}

const mainString = prompt("Enter the main string:");
const subString = prompt("Enter the substring:");

findSubstring(mainString, subString);

using System;

class Program {
    static void Main() {
        Console.Write("Enter the main string: ");
        string mainString = Console.ReadLine();

        Console.Write("Enter the substring: ");
        string subString = Console.ReadLine();

        int pos = mainString.IndexOf(subString);
        if (pos != -1) {
            Console.WriteLine($"Substring found at position: {pos + 1}");
        } else {
            Console.WriteLine("Substring not found.");
        }
    }
}

List of All Programs