Sunday 12 December 2010

Program for To delete n characters from a given position in a given string

Objective:

To delete n characters from a given position in a given string

Description:


//In this program we need to delete a string from the given string at a specified position.



#include<stdio.h>
#include<conio.h>
#include <string.h>
void delchar(char *x,int a, int b);
void main()
{
char string[10];
int  n,pos,p;
clrscr();
puts("Enter the string"); gets(string);
printf("Enter the position from where to delete");
scanf("%d",&pos);
printf("Enter the number of characters to be deleted");
scanf("%d",&n);
delchar(string, n,pos);
getch();
}

// Function to delete n characters
 void delchar(char *x,int a, int b)

{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]); puts(x);
}
}



Output:


1.enter the string nagaraju

Enter the position from where to delete:4
Enter the number of charcters to be deleted 3
 Nagju

2. Enter the string
    kaliraju

     Enter the position from where to delete:0

Enter the number of charcters to be deleted4
Raju


Conclusion: the program is error free

VIVA QUESTIONS:


1) Which command is used to delete the strings ?
Ans: delstr();

2) What are the various types of string functions ?
 Ans: Strcat(), strcpy(), delstr(), substr() ,strlen()etc..,




c programs


No comments:

Post a Comment