Knowee
Questions
Features
Study Tools

Write a program to remove the given character in case of duplicacyFor example, let's say we have s = "Hello", c = 'l', the result will be "Helo"let's say we have s = "Hello World", c = 'l', the result will be "Helo Word"

Question

Write a program to remove the given character in case of duplicacyFor example, let's say we have s = "Hello", c = 'l', the result will be "Helo"let's say we have s = "Hello World", c = 'l', the result will be "Helo Word"

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple C program that removes duplicate occurrences of a given character from a string:

#include <stdio.h>
#include <string.h>

void remove_duplicates(char *s, char c) {
    int index = 0;
    int i;
    int len = strlen(s);
    int freq[256] = {0};

    for(i = 0; i < len; i++) {
        char temp = s[i];
        if(temp == c && freq[temp] != 0) {
            continue;
        }
        freq[temp]++;
        s[index] = s[i];
        index++;
    }
    s[index] = '\0';
}

int main() {
    char s1[] = "Hello";
    char s2[] = "Hello World";
    remove_duplicates(s1, 'l');
    remove_duplicates(s2, 'l');
    printf("%s\n", s1);  // Output: "Helo"
    printf("%s\n", s2);  // Output: "Helo Word"
    return 0;
}

This program works by first initializing an array freq to keep track of the frequency of each character in the string. It then iterates over the string. If it encounters a character that is the one we're removing and its frequency is not zero, it skips it. Otherwise, it increments the frequency of the character, moves the character to the next available position in the string, and increments the index. Finally, it adds a null character at the end of the string to terminate it.

This problem has been solved

Similar Questions

define function remove, given a string and a target character, return a string with all appearances of the target in the original string removed. The original string is not changed. Suppose the original string is "hello, hey" and the target character is 'h', then the returned string is "hello, ey". For the same original string, if the target is 'b', then the return is "hello, hey" (same as the original string). Your tasks: (a) Assume that necessary libraries are included. Define the above function. (b) In main function, write a statement to get the return of calling the above function with arguments "hello, hey: and 'I'. No need to write other parts of main function. (c) Write the expected return of Step (b)

Problem StatementWrite a program to remove the duplicate characters in the given stringInput FormatGiven a stringOutput Formatremove the duplicate characters and print the unique string

java program to remove special characters from the string

Define a function remove_vowel(string1). The function return string1 with all vowels removed. Vowels are any of a, e, i, o, u and A, E, I, O, U.Exampleremove_vowel('python') -> 'pythn'remove_vowel('I love ice cream') -> ' lv c crm'Hintyou can check if a character or an element (not) in a string or a list by using in, or not in.

Given a string s which contains only lower alphabetic characters, check if it is possible to remove at most one character from this string in such a way that frequency of each distinct character becomes same in the string. Return true if it is possible to do else return false.Note: The driver code print 1 if the value returned is true, otherwise 0.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.