Advertisement
Guest User

replchr

a guest
Jun 17th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. /*                                                                              
  2.   replchr.c                                                                    
  3.   S. Edward Dolan                                                              
  4.   Monday, June 17 2019                                                          
  5. */                                                                              
  6.                                                                                
  7. #include <stdio.h>                                                              
  8.                                                                                
  9. /*                                                                              
  10.   Replace all occurances of C with WHAT in SRC.                                
  11.  */                                                                            
  12. void replace(char* src, char c, char* what) {                                  
  13.     char* w;                                                                    
  14.     while (*src) {                                                              
  15.         if (*src == c) {                                                        
  16.             w = what;     /* rewind the pointer */                              
  17.             while (*w)                                                          
  18.                 putchar(*w++);                                                  
  19.             ++src;              /* skip the replaced char in src */            
  20.             continue;           /* and don't print it */                        
  21.         }                                                                      
  22.         putchar(*src++);                                                        
  23.     }                                                                          
  24. }                                                                              
  25.                                                                                
  26. int main(int argc, char** argv) {                                              
  27.     if (argc != 4) {                                                            
  28.         fprintf(stderr,                                                        
  29.                 "Usage: %s SOURCE-STRING CHAR-TO-REPLACE SUBSTITUTE-STRING\n",  
  30.                 argv[0]);                                                      
  31.             return 1;                                                          
  32.     }                                                                          
  33.     replace(argv[1], *argv[2], argv[3]);                                        
  34.     puts("");                                                                  
  35.     return 0;                                                                  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement