How to replace the characters !@#$%^*_+\= in a string with C

1 Answer

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

void replace_special_chars(char *input) {
    char special_chars[] = "!@#$%^*_+=\\";
    for (int i = 0; input[i] != '\0'; i++) {
        if (strchr(special_chars, input[i]) != NULL) {
            input[i] = ' ';
        }
    }
}

int main() {
    char input[] = "The!quick@brown#fox$jumps%^over*_the+\\lazy=dog.";

    printf("Original: %s\n", input);
    
    // Perform character replacement
    replace_special_chars(input);

    printf("Modified: %s\n", input);

    return 0;
}

 
 
/*
run:
  
Original: The!quick@brown#fox$jumps%^over*_the+\lazy=dog.
Modified: The quick brown fox jumps  over  the  lazy dog.
 
*/

 



answered Jun 11, 2025 by avibootz
...