/* Illustrates string copying with arrays
   and pointers */

#include <stdio.h>
#define MAXLINE 1000 /* maximum allowed line length */
 
/*
 * prototypes -- all are different functions that do the same
 * thing: copy a string from old to new
 */
void str1copy(char new[], char old[]);
void str2copy(char new[], char old[]);
void str3copy(char new[], char old[]);
void str4copy(char *new, char *old);

main() {

  char buf[MAXLINE];/* input buffer */
  char new[MAXLINE];/* output buffer */
 
  /*
   * read a line, then write it out
   */
  while(printf("type away!> "),
	fgets(buf, MAXLINE, stdin) != NULL){
    /* #1 to: copy it to the new buffer and print it */
    str1copy(new, buf);
    printf("1> ");
    fputs(new, stdout);
    /* #2: copy it to the new buffer and print it */
    str2copy(new, buf);
    printf("2> ");
    fputs(new, stdout);
    /* #3: copy it to the new buffer and print it */
    str3copy(new, buf);
    printf("3> ");
    fputs(new, stdout);
    /* #4: copy it to the new buffer and print it */
    str4copy(new, buf);
    printf("4> ");
    fputs(new, stdout);
  }
}

void str1copy(char new[], char old[])
{
  int i;
 
  for(i = 0; old[i] != '\0'; i++)
    new[i] = old[i];
  /* tack on the terminator */
  new[i] = '\0';
}
 
void str2copy(char new[], char old[])
{
  int i;/* counter in a for loop */
 
  /*
   * do it char by char
   */
  for(i = 0; old[i]; i++)
    new[i] = old[i];
  /* tack on the terminator */
  new[i] = '\0';
}

void str3copy(char new[], char old[])
{
  int i;/* counter in a for loop */
 
  for(i = 0; new[i] = old[i]; i++);
}

/* With Pointers */ 
void str4copy(char *new, char *old)
{
  while(*new++ = *old++);
}

