
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to delete n characters in a given string
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 5 − Call the function deletestr(str,p,n) jump to step 7
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("
Enter the String:"); gets(str); fflush(stdin); printf("
Enter the position from where the characters are to be deleted:"); scanf("%d",&p); printf("
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
- Related Articles
- Java program to delete duplicate characters from a given String
- C# program to count upper and lower case characters in a given string
- Program to remove duplicate characters from a given string in Python
- C++ Program to Generate a Sequence of N Characters for a Given Specific Case
- Python program to extract characters in given range from a string list
- Concatenating n characters from source string to destination string in C
- C# program to replace n-th character from a given index in a string
- Java program to count upper and lower case characters in a given string
- Program for converting Alternate characters of a string to Upper Case.\n
- C++ Program to Remove all Characters in a String Except Alphabets
- How to delete the vowels from a given string using C language?
- C# Program to remove duplicate characters from String
- C# program to convert a list of characters into a string
- C# program to Count words in a given string
- C++ program to find uncommon characters in two given strings

Advertisements