S18 Answer


/* Puzzle S18 — Replace every instance of one character with another */
#include <stdio.h>
#include <stdlib.h>

void replace( char *str, char old, char new )
{
  while ( *str )
  {
    if ( *str == old ) *str = new;
    str++ ;
  }
}

struct test
{
  char str[50];
  char old;
  char new;
};

int main(int argc, char *argv[])
{
  struct test trials[] =
  {
    {"aaa", 'a', 'b'},
    {"aaa", 'x', 'b'},
    {"abc", 'a', 'b'},
    {"bca", 'a', 'b'},
    {"bca", 'a', 'a'},
    {"aaabbbaaa", 'a', 'b'},
    {"xaxaxa", 'a', 'b'},
    {"XAAxaa", 'a', '*'},
    {"applecart", 'a', 'u'},
    {"blue berry", 'e', 'o'}
 };

  int j;
  for ( j=0; j<sizeof(trials)/sizeof(struct test); j++ )
  {
    printf( "%s\t", trials[j].str );
    replace( trials[j].str, trials[j].old, trials[j].new );
    printf( "%c\t%c\t%s\n",
        trials[j].old, trials[j].new, trials[j].str );
  }

  system("PAUSE");
  return 0;
}

Comments: