How to replace all occurrences of a character in a string with C

1 Answer

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

void char_replace(char *s, char find, char replace) {
    for (int i = 0; i <= strlen(s); i++) {
        if (s[i] == find) {
            s[i] = replace;
        }
    }
}

int main(void) {
    char s[] = "C is a general-purpose, procedural computer programming language";

    char_replace(s, 'a', 'i');
    
    puts(s);
    
    return 0;
}





/*
run:

C is i generil-purpose, proceduril computer progrimming linguige

*/

 



answered Oct 7, 2021 by avibootz
...