home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRREPC.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  97 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /***************************************************************************
  4. * @(#)strrepc
  5. * @(#)      Replaces all occurrences of a character in a string with another
  6. * @(#)      character
  7. *
  8. ***************************************************************************
  9. *@(#)1993 Erik Bachmann
  10. *
  11. * Released to public domain 27-Oct-95
  12. ***************************************************************************/
  13.  
  14. #include <string.h>
  15. #include "bacstd.h"
  16.  
  17. /*
  18.  /-------------------------------------\
  19. |       STRREPC                         |------------------------------------|
  20. |\-------------------------------------/
  21. |
  22. | Replaces all occurrences of a character in a string with another character
  23. |
  24. |----------------------------------------------------------------------------|
  25. | CALL:
  26. |    strrepc( str, cFrom, cTo ) ;
  27. |
  28. | HEADER:
  29. |    string.h
  30. |
  31. | GLOBALE VARIABLES:
  32. |    %
  33. |
  34. | ARGUMENTS:
  35. |    pszStr      : String to be converted
  36. |    cFrom       : Char to be replaced
  37. |    cTo         : Replacement
  38. |
  39. | PROTOTYPE:
  40. |    int _CfnTYPE strrepc( char *pszStr, char cFrom, char cTo ) ;
  41. |
  42. | RETURN VALUE:
  43. |    iReturn     : No of replacements
  44. |
  45. | MODULE:
  46. |    strrepc.c
  47. |----------------------------------------------------------------------------|
  48. |
  49. |
  50. |----------------------------------------------------------------------------|
  51. |1992-11-09/Erik Bachmann
  52. \---------------------------------------------------------------------------|*/
  53.  
  54. int _CfnTYPE strrepc(char *pszStr, char cFrom, char cTo)
  55. {
  56.       char  *ptr ;                              /* Pointer to string */
  57.       int   iReturn = 0 ;                       /* No of replacements */
  58.  
  59.       /*----------------------------------------------------------------*/
  60.  
  61.       while( 0 != ( ptr = strchr( pszStr, cFrom ) ) )
  62.  
  63.       {     /* WHILE cFrom occurs in pszStr */
  64.  
  65.             pszStr[ (int) ptr - (int) pszStr ] = cTo ;
  66.  
  67.             /*- Replace next cFrom with cTo */
  68.  
  69.             iReturn++ ;                                           /*- count */
  70.       }
  71.  
  72.       return( iReturn ) ;
  73. }
  74.  
  75. #ifdef      TEST
  76.  
  77. int main()
  78. {
  79.       char streng[20];
  80.  
  81.       /*-----------------------------------------*/
  82.  
  83.       strcpy(streng, "abcabcabc");
  84.  
  85.       fprintf(stderr, "strrepc()\n\n");
  86.  
  87.       fprintf(stderr, "Replacing c with C\n\nBefore : %s\n\n", streng);
  88.  
  89.       strrepc(streng, 'c', 'C');
  90.  
  91.       fprintf(stderr, "After  : %s\n\n", streng);
  92.  
  93.       return( 0 ) ;
  94. }
  95.  
  96. #endif
  97.