How to replace the first N characters in a string in C

2 Answers

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

int main(void) {
    char s[64] = "pro c programming";
    char dest[64] = "";

    int N = 3;
    strcpy(s, (strcat(strcpy(dest, "XYZ"), s + N)));

    puts(s);

    return 0;
}




/*
run:

XYZ c programming

*/

 



answered Jun 12, 2022 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(void) {
    char s[64] = "pro c programming";
    char replace[4] = "XYZ";

    int N = 3;
    for (int i = 0; i < N; i++)
        s[i] = replace[i];

    puts(s);

    return 0;
}




/*
run:

XYZ c programming

*/

 



answered Jun 12, 2022 by avibootz

Related questions

1 answer 100 views
1 answer 172 views
1 answer 128 views
1 answer 116 views
1 answer 106 views
1 answer 114 views
...