Inserting Character/String into an String.

To insert any Character/String in a String, we need:

  1. A character/string that is to be inserted in the string (say “ch”)
  2. A position/index of the Character/String where it is to be inserted. (say “k”)

Below is the implementation of the above approach:

C++
// CPP code for Inserting character/string into an String.
#include <iostream>
#include <string>

using namespace std;

// Function to demonstrate insert
void insertDemo(string str, string ch, int k)
{

    // Inserts ch at kth index of str
    str.insert(k, ch);
    cout << "Modified String : " << str << endl;
}

// Driver code
int main()
{
    string str("GeeksGeeks ");
    string ch = "for";
    int k = 5;

    cout << "Original String : " << str << endl;
    insertDemo(str, ch, k);

    return 0;
}
C
#include <stdio.h>
#include <string.h>

void insertDemo(char* str, const char* ch, int k) {
    int len1 = strlen(str);
    int len2 = strlen(ch);

    // Shift characters to the right to make space for ch
    for (int i = len1; i >= k; i--) {
        str[i + len2] = str[i];
    }

    // Insert ch at kth index of str
    for (int i = 0; i < len2; i++) {
        str[k + i] = ch[i];
    }

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

int main() {
    char str[] = "GeeksGeeks ";
    char ch[] = "for";
    int k = 5;

    printf("Original String: %s\n", str);
    insertDemo(str, ch, k);

    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        String str = "GeeksGeeks ";
        String ch = "for";
        int k = 5;

        System.out.println("Original String: " + str);
        insertDemo(str, ch, k);
    }

    // Function to demonstrate insert
    public static void insertDemo(String str, String ch, int k) {
        // Inserts ch at kth index of str
        StringBuilder sb = new StringBuilder(str);
        sb.insert(k, ch);
        String modifiedString = sb.toString();
        System.out.println("Modified String: " + modifiedString);
    }
}
Python
# Python program for the above approach

# Function to demonstrate insert
def insert_demo(s, ch, k):

    # Inserts ch at kth index of s
    modified_string = s[:k] + ch + s[k:]
    print("Modified String:", modified_string)

# Driver code
if __name__ == "__main__":
    original_string = "GeeksGeeks "
    ch_to_insert = "for"
    index_to_insert = 5

    print("Original String:", original_string)
    insert_demo(original_string, ch_to_insert, index_to_insert)

# This code is contributed by Susobhan Akhuli
C#
using System;

class Program
{
    // Function to demonstrate insert
    static string InsertDemo(string str, string ch, int k)
    {
        // Inserts ch at kth index of str
        return str.Insert(k, ch);
    }

    // Driver code
    static void Main()
    {
        string str = "GeeksGeeks ";
        string ch = "for";
        int k = 5;

        Console.WriteLine("Original String: " + str);
        string modifiedString = InsertDemo(str, ch, k);
        Console.WriteLine("Modified String: " + modifiedString);
    }
}
JavaScript
// JavaScript equivalent of the given Java code

// Function to demonstrate insert
function insertDemo(str, ch, k) {
    // Inserts ch at kth index of str
    let modifiedString = str.slice(0, k) + ch + str.slice(k);
    console.log("Modified String: " + modifiedString);
}

// Main function
function main() {
    let str = "GeeksGeeks ";
    let ch = "for";
    let k = 5;

    console.log("Original String: " + str);
    insertDemo(str, ch, k);
}

// Call the main function
main();

Output
Original String : GeeksGeeks 
Modified String : w3wiki 

Basic String Operations with Implementation

In this post, we will look into some of the basic String operations such as:

  • Accessing characters by index in a string.
  • Inserting character into a String.
  • Modifying character in String
  • Deletion of Character in String
  • Concatenating strings (combining multiple strings into one).
  • Finding the length of a string
  • Comparing strings for equality or lexicographical order

Let us consider the basic String operations one by one.

Similar Reads

Accessing characters by index in a string.

To access any character in a String, we need:...

Inserting Character/String into an String.

To insert any Character/String in a String, we need:...

Modifying character in String

To modify any Character in a String, we need:...

Deletion of character in String

To delete any Character in a String, we need:...

Concatenating strings (combining multiple strings into one).

To concatenate any String to a String, we need:...

Finding the length/size of a string

To find the length of the String, we need:...

Comparing Strings for Equality

To compare strings, Define a function to compare values with the following conditions :...

Contact Us