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 = “Beginner”, ch = ‘A’, pos = 3
Output: GeeAksInput: 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:
- Initialize a character array or empty string to store the modified string.
- Traverse the original string character by character.
- If the current index matches the specified position, insert the new character.
- Otherwise, copy the current character from the original string to the modified string.
- Null-terminate the modified string.
Below is the implementation of the above approach:
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
// Sample Input
string str = "Beginner";
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[] = "Beginner";
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 = "Beginner";
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 = "Beginner"
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 = "Beginner";
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.