-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinsert_char_or_word.java
More file actions
40 lines (38 loc) · 1.12 KB
/
insert_char_or_word.java
File metadata and controls
40 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//29.Program to Insert Character/Word in any Desired Location in a String
import java.util.*;
public class insert_char_or_word
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String :");
char chr1[]=sc.next().toCharArray();
System.out.println("Enter the word/character you want to insert :");
char chr2[]=sc.next().toCharArray();
System.out.println("Enter the position in which you want to insert");
int pos=sc.nextInt();
pos-=2;
char chr3[]=new char[chr1.length+chr2.length];
int i=0,j=0;
for(;i<=pos;i++)
{
chr3[j]=chr1[i];
j++;
}
for(int k=0;k<chr2.length;k++)
{
chr3[j]=chr2[k];
j++;
}
for(;i<chr1.length;i++)
{
chr3[j]=chr1[i];
j++;
}
System.out.println("Enter the new string is :");
for(int l=0;l<chr3.length;l++)
{
System.out.print(chr3[l]);
}
}
}