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[] =...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the 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 the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
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.
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...
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 function void reverse(char * s) that reverses the string passed as an argument. Your...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your code should use pointer arithmetic (it may increment and decrement pointers, but it may not use array indexing). Here is a piece of code that shows the behavior of reverse: char buf[100]; strcpy(buf, “hello”); reverse(buf); printf(“%s\n”, buf); // output should be olleh
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT