fork download
  1. #include <stdio.h>
  2.  
  3. int change_char( char *str, const char a, const char b) {
  4. int n = 0;
  5. if ( str ) {
  6. while ( *str ) {
  7. if ( *str == a ) {
  8. *str = b; // Can I modify the value pointed by the pointer?
  9. ++n;
  10. }
  11. ++str; // Can I modify the pointer?
  12. }
  13. }
  14. return n;
  15. }
  16.  
  17. int main(void) {
  18. char *test;
  19. test = malloc(13);
  20. strcpy(test,"Hello world!");
  21. change_char(test, 'o', 'X');
  22. printf("%s\n",test); // it will output HellX wXrld!
  23.  
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
HellX wXrld!