Program in C to delete a Character from a given position in a given string Using Function
First Method
#include<stdio.h> #include<string.h> char* Delete(char str[],int p) { int i; for(i=p-1;i<strlen(str);i++) str[i]=str[i+1]; return str; } int main() { char str[100],*str1; int position; printf("Enter a string:-"); scanf("%s",str); printf("Enter the position from where you want to delete a character:-"); scanf("%d",&position); printf("Before deletion = %s\n",str); str1=Delete(str,position);//Here str passing call by reference. Then any changes made inside the Delete function it is also reflected in main Function. printf("After deletion = %s\n",str1);//In deletion operation return 0; }
Output:-
Second Method
#include<stdio.h> #include<string.h> void Delete(char str[],int p) { int i; for(i=p-1;i<strlen(str);i++) str[i]=str[i+1]; } int main() { char str[100],*str1; int position; printf("Enter a string:-"); scanf("%s",str); printf("Enter the position from where you want to delete a character:-"); scanf("%d",&position); printf("Before deletion = %s\n",str); Delete(str,position); printf("After deletion = %s\n",str); return 0; }
Output:-
Related Programs
- Program in C to delete a Character from a given position in a given string
- Program in C to insert a character in string at given position
- Program in C to delete a Character from a given position in a given string Using Function
0 Comments