Write a C program that uses functions to delete n Characters from a given position in a given string.
Write a C program that uses functions to delete n Characters from a given position in a given string.
C Program
//delete n Characters from a given position in a given string.
#include <stdio.h>
#include <string.h>
int main() {
char str[20],finalStr[20];
int position,n;
// Input a string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Input the position
printf("Enter the position from where you want to delete: ");
scanf("%d", &position);
// Input the no.of characters to delete
printf("Enter the number of characters to delete: ");
scanf("%d", &n);
// Check if the position is valid
int strLen = strlen(str);
if (position < 0 || position > strLen) {
printf("Invalid position.\n");
return 1;
}
strncpy(finalStr,str,position); // Copy string up to position
finalStr[position] = '\0'; // Important null terminate
strcat(finalStr,str+position+n); // Concatenate remaining characters
// Print the modified string
printf("Modified string: %s\n", finalStr);
return 0;
}
OUTPUT
Enter a string: Study at a Glance
Enter the position from where you want to delete: 5
Enter the number of characters to delete: 6
Modified string: StudyGlance
๐ป Online C Compiler
Run and test this C program online. You can also edit the code before executing it.
๐ก Edit the program in the compiler and click
Copy Program
to copy the latest edited code.
Related Lab Programs
๐ป
Practice Sessions
1.
Write a simple program that prints the results of all the operators available in C.
View Solution โ
2.
Write a simple program that converts one given data type to another using auto conversion and casting.
View Solution โ
๐ข
Simple Numeric Problems
3.
Write a program to display the class awarded based on the percentage obtained by a student.
View Solution โ
5.
Write a program that shows the binary equivalent of a given positive number between 0 to 255.
View Solution โ
โ
Expression Evaluation
4.
Write a C program to find the sum of individual digits and test whether the number is palindrome.
View Solution โ
๐งฎ
Arrays, Pointers and Functions
1.
Write a C program to find the minimum, maximum and average in an array of integers.
View Solution โ
2.
Write a C program to find the mean, variance and standard deviation of an array and sort the array.
View Solution โ
4.
Write a C program that uses functions to perform Multiplication of two matrices.
View Solution โ
5.
Write a C program to find the transpose of a matrix using dynamically allocated memory.
6.
Write a C program to find factorial using recursive and non-recursive functions.
View Solution โ
7.
Write a C program to find the greatest common divisor using recursive and non-recursive functions.
View Solution โ
8.
Write a C program to calculate x raised to the power of n using recursive and non-recursive functions.
View Solution โ
๐
Files
2.
Write a C program which copies one file to another, replacing lowercase characters with uppercase equivalents.
3.
Write a C program to count occurrences of a character in a text file using command line arguments.
4.
Write a C program to create and update a binary file using fseek.
๐ค
Strings
1.
Write a C program to convert a Roman numeral in the range I to L into its decimal equivalent.
2.
Write a C program to convert a number between 1 and 50 into its Roman numeral equivalent.
3.
Write a C program that uses functions to insert a sub-string into a given main string.
View Solution โ
๐
Miscellaneous
1.
Write a menu-driven program to find the smallest, largest, sum and average.
2.
Write a C program to construct pyramids using stars or numbers.
๐