|
|
| 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
|
| |
c
|
|
|
|
|
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. |
/* 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');
}
|
|
|