Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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 −
- Step 1 − Read the input string
- Step 2 − Read the starting position for deletion
- Step 3 − Read the number of characters to delete
- Step 4 − Iterate through the string and skip n characters from the specified position
- Step 5 − Shift remaining characters to fill the gap
- 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:
ifor reading andjfor writing - When
ireaches the deletion position, it skipsncharacters - 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 ofgets()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.
Advertisements
