In: Computer Science
[ Write in C, not C++]
Define a function char* deleteSymbol(char *s, char x) that
removes the
character x from string s. For s[] = “America”, a call to
deleteSymbol(s, ‘a’)
converts s[] = “Ame”
The fully commented program is:
#include<stdio.h>
//function to delete the symbol from the string
char* deleteSymbol(char*s, char x){
//iterate the character array s
for(int i = 0; s[i] != '\0'; ++i){
//if the current character is x
if(s[i] == x){
int j;
//shift the string to left by 1 place
for(j = i; s[j] != '\0'; ++j){
s[j] = s[j + 1];
}
s[j] = '\0';
i--;
}
}
return s;
}
int main(){
//string to be manipulated
char s[1000];
//character to remove
char x;
// read the input string
printf("Enter the string to manipulate\n");
fgets(s, sizeof(s), stdin);
//read the character to be removed from the string
printf("Enter the character to remove from the string\n");
scanf("%c", &x);
//calling deleteSymbol function to delete symbol from string
deleteSymbol(s, x);
//printing the string after removal of symbol
printf("The string after removal of %c is %s", x, s);
return 0;
}
Output is:
If this answer helps you, do upvote as it motivates us a lot!