Program to Insert Sub String at Specific Location

Program to Insert Sub String at Specific Location

  • Write a program to Insert Sub String at Specific Location in C
  • Write a program to Insert Sub String at Specific Location in C++
  • Write a program to Insert Sub String at Specific Location in Python
  • Write a program to Insert Sub String at Specific Location in PHP
  • Write a program to Insert Sub String at Specific Location in Java
  • Write a program to Insert Sub String at Specific Location in Java Script
  • Write a program to Insert Sub String at Specific Location in C#

Explanation:

You can split a string at a specified point and then concatenate the segments with the new substring in between to add a substring at that location. The sequential reasoning to accomplish this is as follows:

Logic Explanation:

  1. Identify the Position:
    • Determine the index at which you want to insert the substring.
  2. Split the Original String:
    • Split the original string into two parts:
      • The part before the insertion position.
      • The part after the insertion position.
  3. Insert the Substring:
    • Concatenate the first part, the substring to insert, and the second part.
  4. Return the Modified String:
    • The resulting string will be the original string with the substring inserted at the specified location.

Program to Insert Sub String at Specific Location

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

void insertSubstring(char *str, const char *sub, int pos) {
    char temp[200];
    int len = strlen(str);

    if (pos > len) pos = len;

    strncpy(temp, str, pos);
    temp[pos] = '\0';
    strcat(temp, sub);
    strcat(temp, str + pos);
    strcpy(str, temp);
}

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

    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';

    printf("Enter the position to insert: ");
    scanf("%d", &pos);

    insertSubstring(str, sub, pos);

    printf("Modified string: %s\n", str);

    return 0;
}

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

void insertSubstring(string &str, const string &sub, int pos) {
    if (pos > str.length()) pos = str.length();
    str.insert(pos, sub);
}

int main() {
    string str, sub;
    int pos;

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

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

    cout << "Enter the position to insert: ";
    cin >> pos;

    insertSubstring(str, sub, pos);

    cout << "Modified string: " << str << endl;

    return 0;
}

def insert_substring(main_string, sub_string, position):
    return main_string[:position] + sub_string + main_string[position:]

main_string = input("Enter the main string: ")
sub_string = input("Enter the substring: ")
position = int(input("Enter the position to insert: "))

result = insert_substring(main_string, sub_string, position)
print("Modified string:", result)

<?php
function insertSubstring($mainString, $subString, $position) {
    return substr($mainString, 0, $position) . $subString . substr($mainString, $position);
}

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

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

echo "Enter the position to insert: ";
$position = intval(fgets(STDIN));

$result = insertSubstring($mainString, $subString, $position);
echo "Modified string: " . $result . "\n";
?>

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();

        System.out.print("Enter the position to insert: ");
        int position = scanner.nextInt();

        if (position > mainString.length()) {
            position = mainString.length();
        }

        String result = mainString.substring(0, position) + subString + mainString.substring(position);
        System.out.println("Modified string: " + result);
    }
}

function insertSubstring(mainString, subString, position) {
    return mainString.slice(0, position) + subString + mainString.slice(position);
}

const mainString = prompt("Enter the main string:");
const subString = prompt("Enter the substring:");
const position = parseInt(prompt("Enter the position to insert:"));

const result = insertSubstring(mainString, subString, position);
console.log("Modified string:", result);

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();

        Console.Write("Enter the position to insert: ");
        int position = int.Parse(Console.ReadLine());

        if (position > mainString.Length) {
            position = mainString.Length;
        }

        string result = mainString.Insert(position, subString);
        Console.WriteLine("Modified string: " + result);
    }
}

List of All Programs