Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



C Programming Faqs: Frequently Asked Questions    (ISBN: 0201845199)


 

 List Price: $34.99
 Our Price: $34.99
 Used Price: $23.64

 Release Date: January, 1996
 Manufacturer: Addison-Wesley Pub Co (Paperback)
 Sales Rank: 81,838

 Author: Steve Summit, Deborah Lafferty









More Info

 String Copy 2002-01-01 18:08:04
 
Category: source:c:strings
Description: Function to copy a string.
Platform: unix/win
Author: detour
Viewed: 10255
Rating: 3.7/5 (42 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
strcopy.c
/* 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');

}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?