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 some...


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
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: strcpy() and strncpy() funtions in C++
strcpy() and strncpy() funtions in C++
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/strcpy-and-strncpy-funtions-in-c.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/strcpy-and-strncpy-funtions-in-c.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content