Delete occurrence of Character in String using Pointer coderinme

Delete occurrence of Character in String using Pointer coderinme

Delete occurrence of Character in String using Pointer coderinme

In this program, a pointer receives a string – set of different characters. Also, the pointer receives a character as an argument. What we have to do here is very simple and easy. We just have to delete all occurrences of this argument character from the argument string. The function should return the corrected string with no holes. The new string we get as an output should remain the same except the entered character. No other changes are allowed. Let’s code for this program.
This C Program removes given word from a string. Here is the source code of the C Program to remove given word from a string. The C program is successfully compiled and run. The program output is also shown below.

#include<stdio.h>
void str_replace(char * p, char search) {
char *c,*d;
int i;
while( *p != '\0') {
if(*p == search) {
c = p;
i = 1;
while( *c != '\0') {
*c = *(c+1);
c = p + i;
i++;
}
}
p++;
}
}
int main()
{
char word[150],ch;
printf("Enter a string: ");
gets(word);
printf("Enter the character you want to remove: ");
scanf("%c",&ch);
str_replace(word,ch);
printf("Output String: ");
puts(word);
getch();
return 0;
}

For more programs in C, visit our C Coder Archives

Delete occurrence of Character in String using Pointer coderinme

 

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.