danielr1996 danielr1996 - 1 year ago 127
C Question

C Replace one character in an char array by another

I have the following C Code

#include <stdio.h>
int main(void){
char c[] = "ABC"
printf("%s ", c);
c[1] = 'B';
printf("%s", c);
return 0;

The output I want is
but the output I get is
. How can I replace the first character in an String / char array?

Answer Source

Indexing in C arrays start from 0. So you have to replace c[1] = 'B' with c[0] = 'B'.

Also, see similar question from today: Smiles in output C++ - I've put a more detailed description there :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download