Question

In: Computer Science

[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...

[ 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”

Solutions

Expert Solution

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!


Related Solutions

Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
Write a complete C program to do the following:(i) Define a function Nodeptr CreateDLL(char str[])which takes...
Write a complete C program to do the following:(i) Define a function Nodeptr CreateDLL(char str[])which takes a string as parameter and creates a Doubly Linked List of characters and returns the pointer to the first node. (ii) Define a function int IsPalindrome(Nodeptr first)to check whether the string represented by the above doubly linked list pointed to by first, is a palindrome or not and return 1/0 accordingly. Do not use any additional data structure.Write a main function to read a...
write a c++ member function that removes the first instance of a specific element in a...
write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a...
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a linked list. After attemtped removal return the SIZE of the linked lost whether or not the removal was successful.
c++ Write the definition of a function named ‘isLower’ that takes as input a char value...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value and returns true if the character is lowercase; otherwise, it returns false.•Print the message “The character xis lowercase” when returned value above is true, and vice versa.
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
Write a function that uses a local char queue and a local char stack to determine...
Write a function that uses a local char queue and a local char stack to determine if its string parameter is a palindrome. Your solution will look like: #include <stack> #include <queue> ... bool isPalindrome(const string& candidate) { stack<char> s; queue<char> q; //add only upper case letters and digits to the stack and the queue //if a letter is not upper case then convert it to upper case   } Note: I need help to write out this problem in C++...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT