Program to Insert a String into Another String

Description

If a string is inserted into a text. Then newer string is the collection 
all characters present in both the string.
Example : 
  Input : 
      text : whatthat
      string : is
      index : 4
  
  Output : whatisthat

C/C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* C program to insert a string into given string */
//Save it as InsertString.c
#include<stdio.h>
#include<string.h>
int main(){
char str[100], insertStr[50], newStr[100];
int i, pos;
printf("Enter a string : ");
gets(str);
printf("Enter a string to be inserted : ");
gets(insertStr);
printf("Enter the position at which string to be inserted : ");
scanf("%d",&pos);
int j=0;
/*Insert elements of string into new string upto index
pos from start */
for(i=0;i<pos;i++){
newStr[j] = str[i];
j++;
}
//Finding length of string to be inserted
int lenInsertStr = strlen(insertStr);
//Insert elements from string to be inserted to new string
for(i=0;i<lenInsertStr;i++){
newStr[j] = insertStr[i];
j++;
}
//Finding length of string
int lengthStr = strlen(str);
//Insert elements from string from index pos to
it's length into new string
for(i=pos;i<lengthStr;i++){
newStr[j] = str[i];
j++;
}
//End new string with null character
newStr[j] = '\0';
//Finally print the new String
printf("The new String is %s", newStr);
return 0;
}
/* C program to insert a string into given string */ //Save it as InsertString.c #include<stdio.h> #include<string.h> int main(){ char str[100], insertStr[50], newStr[100]; int i, pos; printf("Enter a string : "); gets(str); printf("Enter a string to be inserted : "); gets(insertStr); printf("Enter the position at which string to be inserted : "); scanf("%d",&pos); int j=0; /*Insert elements of string into new string upto index pos from start */ for(i=0;i<pos;i++){ newStr[j] = str[i]; j++; } //Finding length of string to be inserted int lenInsertStr = strlen(insertStr); //Insert elements from string to be inserted to new string for(i=0;i<lenInsertStr;i++){ newStr[j] = insertStr[i]; j++; } //Finding length of string int lengthStr = strlen(str); //Insert elements from string from index pos to it's length into new string for(i=pos;i<lengthStr;i++){ newStr[j] = str[i]; j++; } //End new string with null character newStr[j] = '\0'; //Finally print the new String printf("The new String is %s", newStr); return 0; }
/* C program to insert a string into given string */
//Save it as InsertString.c

#include<stdio.h>
#include<string.h>
int main(){

    char str[100], insertStr[50], newStr[100];
    int i, pos;

    printf("Enter a string : ");
    gets(str);

    printf("Enter a string to be inserted : ");
    gets(insertStr);

    printf("Enter the position at which string to be inserted : ");
    scanf("%d",&pos);

    int j=0;

    
    /*Insert elements of string into new string upto index 
      pos from start */
    
    for(i=0;i<pos;i++){
        newStr[j] = str[i];
        j++;
    }

    
    //Finding length of string to be inserted
    
    int lenInsertStr = strlen(insertStr);

    
    //Insert elements from string to be inserted to new string
    
    for(i=0;i<lenInsertStr;i++){
        newStr[j] = insertStr[i];
        j++;
    }

    
    //Finding length of string
    
    int lengthStr = strlen(str);

    
    //Insert elements from string from index pos to 
      it's length into new string
    
    for(i=pos;i<lengthStr;i++){
        newStr[j] = str[i];
        j++;
    }

    
    //End new string with null character
    
    newStr[j] = '\0';
    
    //Finally print the new String
    
    printf("The new String is %s", newStr);

    return 0;
}
Input:
Enter a string : Sanjay Dutt
Enter a string to be inserted : Sunil
Enter the position at which string to be inserted : 6

Output:
The new String is SanjaySunil Dutt

Java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* Java program to insert a string into given string */
//Save it as InsertString.java
import java.io.*;
import java.util.Scanner;
public class InsertString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i, pos;
System.out.println("Enter a string : ");
String str = scanner.nextLine();
System.out.println("Enter a string to be inserted : ");
String insertStr = scanner.nextLine();
System.out.println("Enter the position at which string to be inserted : ");
pos = scanner.nextInt();
int j=0;
//Converting string str to character array
char strArray[] = str.toCharArray();
/* Declaring the new character array of sum of length of
string and string to be inserted*/
char newStrArray[] = new char[str.length()+insertStr.length()];
//Insert elements of string into new string upto index pos from start
for(i=0;i<pos;i++){
newStrArray[j] = strArray[i];
j++;
}
//Finding length of string to be inserted
int lenInsertStr = insertStr.length();
//Converting string to be inserted to character array
char insertStrArray[] = insertStr.toCharArray();
//Insert elements from string to be inserted to new string
for(i=0;i<lenInsertStr;i++){
newStrArray[j] = insertStrArray[i];
j++;
}
//Finding length of string
int lengthStr = str.length();
/* Insert elements from string from index pos to it's length
into new string */
for(i=pos;i<lengthStr;i++){
newStrArray[j] = strArray[i];
j++;
}
/* Finally print the new string after converting
character array into string*/
System.out.println("The new string is : "+ String.valueOf(newStrArray));
}
}
/* Java program to insert a string into given string */ //Save it as InsertString.java import java.io.*; import java.util.Scanner; public class InsertString { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i, pos; System.out.println("Enter a string : "); String str = scanner.nextLine(); System.out.println("Enter a string to be inserted : "); String insertStr = scanner.nextLine(); System.out.println("Enter the position at which string to be inserted : "); pos = scanner.nextInt(); int j=0; //Converting string str to character array char strArray[] = str.toCharArray(); /* Declaring the new character array of sum of length of string and string to be inserted*/ char newStrArray[] = new char[str.length()+insertStr.length()]; //Insert elements of string into new string upto index pos from start for(i=0;i<pos;i++){ newStrArray[j] = strArray[i]; j++; } //Finding length of string to be inserted int lenInsertStr = insertStr.length(); //Converting string to be inserted to character array char insertStrArray[] = insertStr.toCharArray(); //Insert elements from string to be inserted to new string for(i=0;i<lenInsertStr;i++){ newStrArray[j] = insertStrArray[i]; j++; } //Finding length of string int lengthStr = str.length(); /* Insert elements from string from index pos to it's length into new string */ for(i=pos;i<lengthStr;i++){ newStrArray[j] = strArray[i]; j++; } /* Finally print the new string after converting character array into string*/ System.out.println("The new string is : "+ String.valueOf(newStrArray)); } }
/* Java program to insert a string into given string */
//Save it as InsertString.java

import java.io.*;
import java.util.Scanner;

public class InsertString {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
                
        int i, pos;

        System.out.println("Enter a string : ");
        String str = scanner.nextLine();

        System.out.println("Enter a string to be inserted : ");
        String insertStr = scanner.nextLine();

        System.out.println("Enter the position at which string to be inserted : ");
        pos = scanner.nextInt();

        int j=0;

        
        //Converting string str to character array
        
        char strArray[] = str.toCharArray();
        
        
        /* Declaring the new character array of sum of length of 
           string and string to be inserted*/
        
        char newStrArray[] = new char[str.length()+insertStr.length()];
        
        
        //Insert elements of string into new string upto index pos from start
        
        for(i=0;i<pos;i++){
        	newStrArray[j] = strArray[i];
            j++;
        }

        
        //Finding length of string to be inserted
        
        int lenInsertStr = insertStr.length();
        
        
        //Converting string to be inserted to character array
        
        char insertStrArray[] = insertStr.toCharArray();

        
        //Insert elements from string to be inserted to new string
        
        for(i=0;i<lenInsertStr;i++){
        	newStrArray[j] = insertStrArray[i];
            j++;
        }

        
        //Finding length of string
        
        int lengthStr = str.length();

        
        /* Insert elements from string from index pos to it's length 
           into new string */
        
        for(i=pos;i<lengthStr;i++){
        	newStrArray[j] = strArray[i];
            j++;
        }
        
        
    /* Finally print the new string after converting 
       character array into string*/
        
        System.out.println("The new string is : "+ String.valueOf(newStrArray));
    }
}
Input:
Enter a string : 
Sanjay Dutt
Enter a string to be inserted : 
Sunil
Enter the position at which string to be inserted : 
6

Output:
The new string is : SanjaySunil Dutt

Related Programs

1) Program to Append one String to another String
2) Program to remove vowels from a String
3) Program to remove given number from a string
4) Program to Add numbers present in a String
5) Program to find maximum number of 1s in a substring of a String
6) Count Number of Words in a String
7) Find Occurrence Of Each Word In a Sentence
8) Print All Unique Words Of A String
9) Program To Print Unique Word Of A String Using Set
10) Program to Compare Two Strings
Share Me

Leave a Reply