Cách chèn một chuỗi vào một chuỗi khác C++

Sự miêu tả

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++

/* C program to insert a string into given string */
//Save it as InsertString.c

#include
#include
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;iFinding length of string to be inserted
	
    int lenInsertStr = strlen(insertStr);

	
    //Insert elements from string to be inserted to new string
	
    for(i=0;iFinding length of string
	
    int lengthStr = strlen(str);

	
    //Insert elements from string from index pos to 
	  it's length into new string
	
    for(i=pos;iEnd 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

/* 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;iFinding 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;iFinding length of string
		
	    int lengthStr = str.length();

		
	    /* Insert elements from string from index pos to it's length 
		   into new string */
		
	    for(i=pos;iFinally 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

Chương trình liên quan

1) Chương trình nối một Chuỗi vào một Chuỗi khác
2) Chương trình xóa các nguyên âm khỏi Chuỗi
3) Chương trình xóa số đã cho khỏi chuỗi
4) Chương trình thêm các số có trong
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

Làm cách nào để chèn một chuỗi vào một chuỗi khác trong C?

Trong C, hàm strcat() dùng để nối hai chuỗi. .
Lấy chuỗi đích
Tìm ký tự NULL
Sao chép chuỗi nguồn bắt đầu bằng ký tự NULL của chuỗi đích
Nối một ký tự NULL vào chuỗi đích sau khi được sao chép

Làm cách nào để chèn giá trị chuỗi trong C?

Bạn có thể khởi tạo chuỗi theo một số cách. Hãy lấy một ví dụ khác. char c[5] = "abcde"; Ở đây, chúng tôi đang cố gán 6 ký tự (ký tự cuối cùng là '\0' ) cho một char .