String Copy
2002-01-01 18:08:04
Category: c:strings
Description: Function to copy a string.
Author: detour
Viewed: 10761
Rating: (44 votes)


/* strcopy.c written by detour@metalshell.com
 *
 * example of a string copying function.
 *
 * http://www.metalshell.com/
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void strcopy(const char *, char *);
 
int main() {
  char *string1 = strdup("This is string1");
  char *string2 = strdup("This is string2");
 
  strcopy(string1, string2);
  printf("String2: %s", string2);
}
 
void strcopy(const char * str1, char * str2) {
  int x = 0;
 
  /* change the size of string2 to the size of string1 */
  realloc(str2, strlen(str1)+1);
 
  do {
    str2[x] = str1[x];
  } while (str1[x++] != '\0');
 
}