Open In App

How to insert a character in a string?

You have given a string str, a character ch and an integer position pos, the task is to insert the character ch into the string str at the specified position pos.

Examples:

Input: str = "Geeks", ch = 'A', pos = 3
Output: GeeAks

Input: str = "HelloWorld", ch = '!', pos = 5
Output: Hello!World

Approach:

Iterate over the input string and keep inserting each character into a new string until we reach the position where we need to insert the character. After reaching the position where we need to insert the character, simply insert the character at the end of the new string and for all the remaining characters, insert them at the end of the new string.

Step-by-step algorithm:

Below is the implementation of the above approach:

#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    // Sample Input
    string str = "Geeks";
    char ch = 'A';
    int pos = 3;

    string ans;
    // Iterate over the string, and find the position where
    // ch needs to be inserted
    for (int i = 0; i < str.length(); i++) {
        // Insert the character at pos position
        if (i == pos) {
            ans.push_back(ch);
        }
        // For the remaining characters, simply insert them
        // to the answer string
        ans.push_back(str[i]);
    }

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

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

int main()
{
    // Sample Input
    char str[] = "Geeks";
    int len = strlen(str);
    char ans[len + 1];
    char ch = 'A';
    int pos = 3;
    int i, j = 0;
    // Iterate over the string, and find the position where
    // ch needs to be inserted
    for (i = 0; i < len; i++) {
        // Insert the character at pos position
        if (i == pos) {
            ans[j++] = ch;
        }
        // For the remaining characters, simply insert them
        // to the answer string
        ans[j++] = str[i];
    }
    // Null-terminate the string
    ans[j] = '\0';

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

    return 0;
}
public class Main {
    public static void main(String[] args)
    {
        // Sample Input
        String str = "Geeks";
        StringBuilder ans = new StringBuilder();
        char ch = 'A';
        int pos = 3;
        // Iterate over the string, and find the position
        // where ch needs to be inserted
        for (int i = 0; i < str.length(); i++) {
            // Insert the character at pos position
            if (i == pos) {
                ans.append(ch);
            }
            // For the remaining characters, simply insert
            // them to the answer string
            ans.append(str.charAt(i));
        }

        System.out.println("Modified string: "
                           + ans.toString());
    }
}
# Sample Input
str = "Geeks"
ans = ""
ch = 'A'
pos = 3

# Iterate over the string, and find the position where ch needs to be inserted
for i in range(len(str)):
        # Insert the character at pos position
    if i == pos:
        ans += ch
    # For the remaining characters, simply insert them to the answer string
    ans += str[i]

print("Modified string:", ans)
// Sample Input

let str = "Geeks";
let ans = "";
let ch = 'A';
let pos = 3;

// Iterate over the string, and find the position where ch needs to be inserted
for (let i = 0; i < str.length; i++) {
    // Insert the character at pos position
    if (i === pos) {
        ans += ch;
    }
    // For the remaining characters, simply insert
    // them to the answer string
    ans += str[i];
}

console.log("Modified string: " + ans);

Output
Modified string: GeeAks

Time Complexity: O(n) where n is the length of the string.
Auxiliary Space: O(n) where n is the length of the string.

Article Tags :