C Program to delete n characters in a given string

In C programming, deleting n characters from a specific position in a string involves shifting the remaining characters to fill the gap. This operation modifies the original string by removing a substring starting from a given position.

Syntax

void deleteString(char str[], int position, int n);

Algorithm

The algorithm to delete n characters from a given position in a string is as follows −

  1. Step 1 − Read the input string
  2. Step 2 − Read the starting position for deletion
  3. Step 3 − Read the number of characters to delete
  4. Step 4 − Iterate through the string and skip n characters from the specified position
  5. Step 5 − Shift remaining characters to fill the gap
  6. Step 6 − Add null terminator and display the result

Example

The following C program demonstrates how to delete n characters from a given position in a string −

#include <stdio.h>
#include <string.h>

void deleteString(char str[], int position, int n) {
    int i, j;
    int len = strlen(str);
    
    /* Check if position is valid */
    if (position < 1 || position > len) {
        printf("Invalid position!<br>");
        return;
    }
    
    /* Shift characters after deletion */
    for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
        if (i == (position - 1)) {
            i = i + n;  /* Skip n characters */
        }
        str[j] = str[i];
    }
    str[j] = '\0';  /* Null terminate */
}

int main() {
    char str[100];
    int position, n;
    
    printf("Enter the string: ");
    fgets(str, sizeof(str), stdin);
    
    /* Remove newline if present */
    str[strcspn(str, "<br>")] = '\0';
    
    printf("Enter position to start deletion (1-based): ");
    scanf("%d", &position);
    
    printf("Enter number of characters to delete: ");
    scanf("%d", &n);
    
    printf("\nOriginal string: %s<br>", str);
    deleteString(str, position, n);
    printf("String after deletion: %s<br>", str);
    
    return 0;
}
Enter the string: Tutorials Point Programming
Enter position to start deletion (1-based): 11
Enter number of characters to delete: 6
Original string: Tutorials Point Programming
String after deletion: Tutorials Programming

How It Works

  • The function uses two indices: i for reading and j for writing
  • When i reaches the deletion position, it skips n characters
  • Characters before and after the deletion range are copied to fill the gap
  • The string is properly null-terminated after the operation

Key Points

  • Position indexing starts from 1 for user input but internally uses 0-based indexing
  • The function modifies the original string in-place
  • Input validation ensures the position is within valid bounds
  • Using fgets() instead of gets() prevents buffer overflow

Conclusion

Deleting characters from a string in C requires careful index management and character shifting. This approach efficiently removes a substring while maintaining the string's integrity and proper null termination.

Updated on: 2026-03-15T14:12:17+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements