O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

strcpy() and strncpy()

C++ inherits from C a library of functions for dealing with strings. Among the many functions provided are two for copying one string into another: strcpy() and strncpy(). strcpy() copies the entire contents of one string into a designated buffer. Listing 15.8 demonstrates its use.

Listing 15.8. Using strcpy()
 0:  // Listing 15.8 - Using strcpy()
 1:  #include <iostream>
 2:  #include <string.h>
 3:
 4:  int main()
 5:  {
 6:      char String1[] = "No man is an island";
 7:      char String2[80];
 8:
 9:      strcpy(String2,String1);
10:
11:      std::cout << "String1: " << String1 << std::endl;
12:      std::cout << "String2: " << String2 << std::endl;
13:      return 0;
14:  }
String1: No man is an island
String2: No man is an island
					

The header file STRING.H ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required