How to remove quotes and commas from a string in C

3 Answers

0 votes
// Manual Filtering

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

// ------------------------------------------------------------
// removeQuotesAndCommas
// Removes single quotes ('), double quotes ("), and commas (,)
// by manually copying only allowed characters.
// ------------------------------------------------------------
void removeQuotesAndCommas(const char *input, char *output) {
    int j = 0;

    for (int i = 0; input[i] != '\0'; i++) {
        if (input[i] != '\'' && input[i] != '"' && input[i] != ',') {
            output[j++] = input[i];  // keep only allowed characters
        }
    }

    output[j] = '\0'; // null-terminate result
}

int main() {
    const char s[] = "\"Imagine there's, no heaven\", 'Imagine' \"all\", the people.";
    char cleaned[256] = "";

    removeQuotesAndCommas(s, cleaned);

    printf("Original: %s\n", s);
    printf("Cleaned:  %s\n", cleaned);

    return 0;
}


/*
run:

Original: "Imagine there's, no heaven", 'Imagine' "all", the people.
Cleaned:  Imagine theres no heaven Imagine all the people.

*/

 



answered May 10 by avibootz
0 votes
// Pointer-Based Filtering

#include <stdio.h>

// ------------------------------------------------------------
// removeQuotesAndCommas
// Removes single quotes ('), double quotes ("), and commas (,)
// using pointer iteration for efficiency.
// ------------------------------------------------------------
void removeQuotesAndCommas(const char *input, char *output) {
    const char *src = input;
    char *dst = output;

    while (*src) {
        if (*src != '\'' && *src != '"' && *src != ',') {
            *dst++ = *src;  // copy allowed characters
        }
        src++;
    }

    *dst = '\0'; // null-terminate
}

int main() {
    const char s[] = "\"Imagine there's, no heaven\", 'Imagine' \"all\", the people.";
    char cleaned[256] = "";

    removeQuotesAndCommas(s, cleaned);

    printf("Original: %s\n", s);
    printf("Cleaned:  %s\n", cleaned);

    return 0;
}



/*
run:

Original: "Imagine there's, no heaven", 'Imagine' "all", the people.
Cleaned:  Imagine theres no heaven Imagine all the people.

*/

 



answered May 10 by avibootz
0 votes
// Using strchr

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

// ------------------------------------------------------------
// removeQuotesAndCommas
// Removes single quotes ('), double quotes ("), and commas (,)
// using strchr to check if a character is unwanted.
// ------------------------------------------------------------
void removeQuotesAndCommas(const char *input, char *output) {
    const char *unwanted = "'\",";  // characters to remove
    int j = 0;

    for (int i = 0; input[i] != '\0'; i++) {
        if (strchr(unwanted, input[i]) == NULL) {
            output[j++] = input[i];  // keep allowed characters
        }
    }

    output[j] = '\0';
}

int main() {
    const char s[] = "\"Imagine there's, no heaven\", 'Imagine' \"all\", the people.";
    char cleaned[256];

    removeQuotesAndCommas(s, cleaned);

    printf("Original: %s\n", s);
    printf("Cleaned:  %s\n", cleaned);

    return 0;
}



/*
run:

Original: "Imagine there's, no heaven", 'Imagine' "all", the people.
Cleaned:  Imagine theres no heaven Imagine all the people.

*/

 



answered May 10 by avibootz

Related questions

1 answer 109 views
1 answer 100 views
100 views asked Feb 14, 2022 by avibootz
1 answer 141 views
1 answer 104 views
3 answers 148 views
...