Quantcast
Channel: Java Question Bank » Cpp Programs
Viewing all articles
Browse latest Browse all 22

A CPP Program to insert a substring into the given main string

$
0
0

*********************************************************************

A CPP Program to insert a substring into the given main string

********************************************************************

**************

Source Code :

**************

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
program : To insert a substring into the given main string from a given position that uses functions
#include<iostream.h>
#include<conio.h>
#include<process.h>
void insert(char [] ,int );
void del(char [],int);
void main()
{
int pos,ch;
char a[20];
clrscr();
cout<<"Enter main string:\n";
cin>>a;
cout<<"Enter position: ";
cin>>pos;
do
{
cout<<"\n1.Insertion\t2.Deletion\t3.Exit\t";
cout<<"\nEnter ur choice:";
cin>>ch;
switch(ch)
{
case 1:
insert(a,pos);
break;
case 2:
del(a,pos);
break;
case 3:
exit(0);
default:
cout<<"\nInvalid choice";
break;
}
}while(ch<3);
getch();
}
void insert(char a[20],int n)
{
int i,j;
char sub[20],b[30];
cout<<"\nEnter Substing: ";
cin>>sub;
for(i=0,j=0;a[i]!='';i++,j++)
{
if(j==n)
{
for(int k=0;sub[k]!='';k++,j++)
{
b[j]=sub[k];
}
}
b[j]=a[i];
}
b[j]='';
cout<<"\nThe resulted string is: "<<b;
getch();
}
void del(char a[20],int pos)
{
char b[30];
int n,i,j;
cout<<"\nEnter no.of characters to be deleted:";
cin>>n;
for(i=0,j=0;a[i]!='';i++,j++)
{
if(i==pos)
i=i+n;
b[j]=a[i];
}
b[j]='';
cout<<"\nThe resulted string is: "<<b;
getch();
}
/*Output:

Enter main string:
javaqbank
Enter position: 5

1.Insertion     2.Deletion      3.Exit
Enter ur choice:1

Enter Substing: uestion

The resulted string is: javaquestionbank
1.Insertion     2.Deletion      3.Exit
Enter ur choice:2

Enter no.of characters to be deleted:4

The resulted string is: javaquestion
1.Insertion     2.Deletion      3.Exit
Enter ur choice:3                          */

1,793 total views, 2 views today


Viewing all articles
Browse latest Browse all 22

Trending Articles