Copying Strings

There are many library functions for manipulating NULL terminated strings. To use them, you need to include string.h:
#include <string.h>
The first one you'll want to use is strcpy. It copies one string into another. It's declared something like this:
// don't worry about the char * return type
char *strcpy(char destination[], const char source[]);
Example:
char string1[8], string2[8]; // declare array

strcpy(string1, "Celsius");  // string1 = "Celsius"
strcpy(string2, string1);    // string2 = string1
Another one you'll find useful is strcat. It concatenates two strings:
char string1[13];            // declare array of 13

strcpy(string1, "Hello, ");  // string1 = "Hello, "
strcat(string1, "World");    // string1 = "Hello, World"
After the strcat function is called, string1 is:
Hello, World
Previous page
Next page

Back to Lesson 12 Index
Back to Outline