Inserting Character/String into an String.
To insert any Character/String in a String, we need:
- A character/string that is to be inserted in the string (say “ch”)
- A position/index of the Character/String where it is to be inserted. (say “k”)
Below is the implementation of the above approach:
// 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;
}
#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;
}
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 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
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 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.
Contact Us