In C: The code below is supposed to remove the char from the original string, no
ID: 3825676 • Letter: I
Question
In C: The code below is supposed to remove the char from the original string, not just print the other chars. How to fix that? The full code is at the very bottom.
void rmchr(char str[],char ch) //Function Definition
{
char *p; //Pointer Variable
p=str; //Storing address of First character of String in *p
printf("%c ",ch); //Printing Character to remove
while(*p!='') //Loop for iterating String
{
if(*p!=ch)
{ //Checking if Character is not equal to each character
printf("%c",*p); //Prints that unequal character
}
p++; //Address Increment of *p
}
printf(" ");
}
The full code:
#include<stdio.h> //Header Files
int main()
{
void rmchr(char[],char); //Function Declaration String,Character passing into function
char s1[]= "abracadabra"; //s1 to abracadabra
char s2[]= "abracadabra"; //s2 to abracadabra
char s3[]= "abracadabra"; //s3 to abracadabra
char s4[]= "aaaa"; //s4 to aaaa
char s5[]= "aaaa"; //s5 to aaaa
printf("String Character Answer ");
printf("%s ",s1); //Printing String Before Passing
rmchr(s1,'a'); //Passing String and Character to remove into rmchr function
printf("%s ",s2); //Printing String Before Passing
rmchr(s2,'b'); //Passing String and Character to remove into rmchr function
printf("%s ",s3); //Printing String Before Passing
rmchr(s3,'n'); //Passing String and Character to remove into rmchr function
printf("%s ",s4); //Printing String Before Passing
rmchr(s4,'a'); //Passing String and Character to remove into rmchr function
printf("%s ",s5); //Printing String Before Passing
rmchr(s5,'n'); //Passing String and Character to remove into rmchr function
return 0;
}
void rmchr(char str[],char ch) //Function Definition
{
char *p; //Pointer Variable
p=&str[0]; //Storing address of First character of String in *p
printf("%c ",ch); //Printing Character to remove
while(*p!='') //Loop for iterating String
{
if(*p!=ch)
{ //Checking if Character is not equal to each character
printf("%c",*p); //Prints that unequal character
}
p++; //Address Increment of *p
}
printf(" ");
}
Explanation / Answer
#include <stdio.h>
#include <string.h> //Header Files
int main()
{
void rmchr(char[],char); //Function Declaration String,Character passing into function
char s1[]= "abracadabra"; //s1 to abracadabra
char s2[]= "abracadabra"; //s2 to abracadabra
char s3[]= "abracadabra"; //s3 to abracadabra
char s4[]= "aaaa"; //s4 to aaaa
char s5[]= "aaaa"; //s5 to aaaa
printf("String Character Answer ");
printf("%s ",s1); //Printing String Before Passing
rmchr(s1,'a'); //Passing String and Character to remove into rmchr function
printf("%s ",s1);
printf("%s ",s2); //Printing String Before Passing
rmchr(s2,'b'); //Passing String and Character to remove into rmchr function
printf("%s ",s2);
printf("%s ",s3); //Printing String Before Passing
rmchr(s3,'n'); //Passing String and Character to remove into rmchr function
printf("%s ",s3);
printf("%s ",s4); //Printing String Before Passing
rmchr(s4,'a'); //Passing String and Character to remove into rmchr function
printf("%s ",s4);
printf("%s ",s5); //Printing String Before Passing
rmchr(s5,'n'); //Passing String and Character to remove into rmchr function
printf("%s ",s5);
return 0;
}
void rmchr(char str[],char ch) //Function Definition
{
char *p;
int len = strlen(str);
char cp[len];
int clen=0,i; //Pointer Variable
p=&str[0]; //Storing address of First character of String in *p
printf("%c ",ch); //Printing Character to remove
while(*p!='') //Loop for iterating String
{
if(*p!=ch)
{ //Checking if Character is not equal to each character
cp[clen] = *p;
clen++; //Prints that unequal character
}
p++; //Address Increment of *p
}
for(i=0;i<clen;i++)
{
str[i]=cp[i];
}
str[clen]='';
}