# C Program to delete n characters in a given string

CServer Side ProgrammingProgramming

## Problem

Write the user functions to Delete N – Characters from Position in a given string. Here, the string is given by the user at runtime.

## Solution

The solution to delete n characters in a given string is as follows −

## Algorithm

Refer an algorithm to delete n characters in a given string.

Step 1 − Start

Step 2 − Read string at runtime

Step 3 − Read position from where we need to delete the characters

Step 4 − Read n, number of characters to delete from that position

Step 6 − Stop

Step 7 − Called function deletestr(str,p,n)

1. for i =0 , j = 0 to Length[str]
2. do if i = p-1
3. i = i + n
4. str[j] =str[i]
5. str[j] = NULL
6. print str

## Example

Following is the C program to delete n characters in a given string

#include <stdio.h>
#include <conio.h>
// prototype of function
void del_str(char [],int, int);
main(){
int n,p;
char str[30];
printf("\n Enter the String:");
gets(str);
fflush(stdin);
printf("\n Enter the position from where the characters are to be deleted:");
scanf("%d",&p);
printf("\n Enter Number of characters to be deleted:");
scanf("%d",&n);
del_str(str,p,n);
}
//function call
void del_str(char str[],int p, int n){
int i,j;
for(i=0,j=0;str[i]!='\0';i++,j++){
if(i==(p-1)){
i=i+n;
}
str[j]=str[i];
}
str[j]='\0';
puts(" The string after deletion of characters:");
puts(str);
}

## Output

When the above program is executed, it produces the following result −

Enter the String:Tutorials Point C programming

Enter the position from where the characters are to be deleted:10

Enter Number of characters to be deleted:6
The string after deletion of characters:
Tutorials C programming
Updated on 31-Aug-2021 13:21:34