Program in C for inserting a string in the middle of string and deleting a string from the string


#include<stdio.h>
#include<conio.h>
void insert(char s[],char ss[],int pos)
{
char temp [10];
int i,j;                                                      
j=0;                                                         
for(i=pos;s[i]!='\0';i++)                                               
temp[j++]=s[i];                                                           
temp[j]='\0';                                                                
for(i=0;ss[i]!='\0';i++)
s[pos++]=ss[i];
for(i=0;temp[i]!='\0';i++)
s[pos++]=temp[i];
s[pos]='\0';
}
void del(char s[15],int pos,int count)
{
int i,j;
for(i=pos,j=pos+count;s[j]!='\0';i++,j++)  
s[i]=s[j];                                                                                   
s[i]='\0';
}
void main()
{
char str1[100],str2[20];
int pos,len,ch;
clrscr();
printf("\n Enter the String:-");
gets(str1);
while(1)
{
printf("\n1...Insert\n2...Delete\n3...Exit\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the String to be added:-");
scanf("%s",&str2);
printf("\nenter the pos: ");
scanf("%d",&pos);
insert(str1,str2,pos);
printf("string after insertion:");
puts(str1);
break;
case 2:
printf("\nenter the pos: ");
scanf("%d",&pos);
printf("\nenter the len: ");
scanf("%d",&len);
del(str1,pos,len);
printf("string after deletion:");
puts(str1);
break;
case 3:
printf("\nBye....");
getch();
exit(0);
default:
printf("Enter correct value:");
}
}
}

Output:


About the Author

Unknown

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

Post a Comment

 
My Study Voice © 2015 - Designed by Templateism | Distributed By Blogger Templates