Latest

recent

strcpy() and strncpy() funtions in C++


The second most popular function in string.h probably was strcpy(), which copied one string to another. This may now be diminished somewhat as C-style null-terminated strings have become less important in C++; typically, string manipulation is done from within a vendor-supplied or user-written string class. Nonetheless, your string class must support an assignment operator and a copy constructor, and often these are implemented using strcpy(), as illustrated in Listing 21.2.

Listing 21.2. Using strcpy.

1: #include <iostream.h>
2: #include <string.h>
3:
4: int main()
5: {
6: char stringOne80];
7: char stringTwo80];
8:
9: stringOne0]='\0';
10: stringTwo0]='\0';
11:
12: cout << "String One: " << stringOne << endl;
13: cout << "String Two: " << stringTwo << endl;
14:
15: cout << "Enter a string: ";
16: cin.getline(stringOne,80);
17:
18: cout << "\nString One: " << stringOne << endl;
19: cout << "String Two: " << stringTwo << endl;
20:
21: cout << "copying..." << endl;
22: strcpy(stringTwo,stringOne);
23:
24: cout << "\nString One: " << stringOne << endl;
25: cout << "String Two: " << stringTwo << endl;
26: cout << "\nDone " << endl;
27: return 0;
28: }

Output: String One:
String Two:
Enter a string: Test of strcpy()

String One: Test of strcpy()
String Two:
copying...

String One: Test of strcpy()
String Two: Test of strcpy()

Done

Analysis: Two C-style null-terminated strings are declared on lines 6 and 7. They are initialized to empty on lines 9 and 10, and their values are printed on lines 12 and 13. The user is prompted to enter a string, and the result is put in stringOne; the two strings are printed again, and only stringOne has the input. Strcpy() is then called, and stringOne is copied into stringTwo.

Note that the syntax of strcpy() can be read as "copy into the first parameter the string in the second parameter." What happens if the target string (stringTwo) is too small to hold the copied string? This problem and its solution are illustrated in Listing 21.3.

Listing 21.3. Using strncpy().

1: #include <iostream.h>
2: #include <string.h>
3:
4: int main()
5: {
6: char stringOne[80];
7: char stringTwo[10];
8: char stringThree[80];
9:
10: stringOne[0]='\0';
11: stringTwo[0]='\0';
12: stringThree[0]='\0';
13:
14: cout << "String One: " << stringOne << endl;
15: cout << "String Two: " << stringTwo << endl;
16: cout << "String Three: " << stringThree << endl;
17:
18: cout << "Enter a long string: ";
19: cin.getline(stringOne,80);
20: strcpy(stringThree,stringOne);
21: // strcpy(stringTwo,stringOne);
22:
23: cout << "\nString One: " << stringOne << endl;
24: cout << "String Two: " << stringTwo << endl;
25: cout << "String Three: " << stringThree << endl;
26:
27: strncpy(stringTwo,stringOne,9);
28:
29: cout << "\nString One: " << stringOne << endl;
30: cout << "String Two: " << stringTwo << endl;
31: cout << "String Three: " << stringThree << endl;
32:
33: stringTwo[9]='\0';
34:
35: cout << "\nString One: " << stringOne << endl;
36: cout << "String Two: " << stringTwo << endl;
37: cout << "String Three: " << stringThree << endl;
38: cout << "\nDone." << endl;
39: return 0;
40: }

Output: String One:
String Two:
String Three:
Enter a long string: Now is the time for all...

String One: Now is the time for all...
String Two:
String Three: Now is the time for all...

String One: Now is the time for all...
String Two: Now is th_+||
String Three: Now is the time for all...

String One: Now is the time for all...
String Two: Now is th
String Three: Now is the time for all...

Done.

Analysis: On lines 6, 7, and 8, three string buffers are declared. Note that stringTwo is declared to be only 10 characters, while the others are 80. All three are initialized to zero length on lines 10 to 12 and are printed on lines 14 to 16.

The user is prompted to enter a string, and that string is copied to string three on line 20. Line 21 is commented out; copying this long string to stringTwo caused a crash on my computer because it wrote into memory that was critical to the program.
The standard function strcpy() starts copying at the address pointed to by the first parameter (the array name), and it copies the entire string without ensuring that you've allocated room for it!
The standard library offers a second, safer function, strncpy(), which copies only a specified number of characters to the target string. The n in the middle of the function name strncpy() stands for number. This is a convention used throughout the standard libraries.
On line 27, the first nine characters of stringOne are copied to stringTwo and the result is printed. Because strncpy() does not put a null at the end of the copied string, the result is not what was intended. Note that strcpy() does null-terminate the copied string, but strncpy() does not, just to keep life interesting.
The null is added on line 33, and the strings are then printed a final time.

Goto String Standard Library to get all the String functions
strcpy() and strncpy() funtions in C++ Reviewed by 1000sourcecodes on 21:42 Rating: 5
Powered by Blogger.