Single characters can be replaced in a string. Given the following declarations, several things are possible.
char str[25] = “cot”;
char ch = ‘u’;
char D[25] = “pat”;
- Replacing a single character using a char variable:
- D[1] = ch;
This would result in D containing “put”.
- Replacing a single character using a char literal:
- D[1] = ‘e’;
This would result in D containing “pet”.
- Replacing a single character using a single character from a string
variable:
- D[1] = str[1];
This would result in D containing “pot”.