C++ String Copy

Definition of C++ String Copy

String sao chép is a function in C++ which is used for copying one entire string to another string seamlessly without making many changes and efforts. string sao chép in C++ is part of the standard library function which makes the content of one string, in fact, the entire string copied into another string. Unlike C where the string sao chép function is present in the string. h header file, In C++ the string sao chép function is present in the cstring header file. All the contents present within the source string gets fully copied in the destination string using string sao chép easily.

Syntax:

Start Your miễn phí PM Development Course

Website development, programming languages, PM testing & others

The syntax flow for the C++ String Copy is as shown:

string_1.sao chép(string_2,len_gth);
string_1.sao chép(string_2,len_gth,posi_tion);

string_1 and string_2 are the two objects which are considered as the source and the destination strings. Let’s see how the Sting sao chép functions with this.

Parameters:

The parameters passed signifies the following:

  • String_2 is the destination string which is the destination object comprising of the data and it persists the copies string from the source string.
  • String_1 represents the source string.
  • len_gth represents the length of the substring in the entire string.
  • posi_tion tells for the position of the very first position of the character to include its value.

How to Copy String in C++?

Copy string functionality is the same as other functionality in C++ despite being the fact that in C standard library containing the Copy functionality is present within the string. header whereas in C ++ the header function comes with the standard library function of cstring.h. The method must mandatorily contain the source string and the destination string as part of the argument for manipulation and for copying the content entirely from source to destination. The source string is the string that will get copied into the destination string. Destination string is the string that will comprise of the content that will get copied or the entire string from the source string. Even the parameters include the length which means the substring for the string defined in the source or the destination string. Also, the parameter may include the position from where the string value will get fetched.

Xem Thêm  Active Minitool Partition Wizard By Serial Key

There is a misconception that the source string gets appended to the destination string, but the fact is completely different it never works in this fashion rather it copies the entire content without any appending from the source string to the destination string. Even the source string does not get altered or changed which means it not at all get affected and it remains the same as earlier. The return value for the copied string in C++ includes a number of characters that get copied. The behaviors remain undefined if in case the memory allocated for the destination pointer pointing towards the destination string is not that much larger to accommodate all the content from the source string. Moreover, the string should not be in a format where they will overlap each other. Both string functions must be kept in mind while the content sao chép or content manipulation.

There is a myth being created by many programmers that the strncpy and strcpy function can be used alternatively but it is not at all true in case the memory allocation size gets increased or decreased then both the strcpy and strncpy functions will be used simultaneously but this is wrong. The strcpy() function satisfies the condition where the source string gets copied to the destination string and if the memory allocation size of the destination string is more than the source string then the source string easily gets copied to the destination string including terminating null characters and thus another case gets arise where the destination string might get less memory allocation comparatively then, in this case, the terminating null character will be skipped and the content from the source string to the destination string will be copied which means the terminating null character will get neglected. Also, the string will not overlap the destination string thus the destination string must have more space with proper memory allocation.

Xem Thêm  Thẻ HTML - thẻ tiêu đề html không hoạt động</span></div></a></div><h3>Examples of C++ String Copy</h3> <p>Following are the examples are given below:</p> <h4>Example #1</h4> <p>This program demonstrates the copying of the contents of the source string to the destination string using the strcpy() function which is part of the standard library function of string.h header file as shown in the output.</p> <p><strong>Code:</strong></p> <p><code>using namespace std;<br /> intmain()<br /> {<br /> string src_str1 = "Learning_New_Language";<br /> char dest_on[30];<br /> coutvàlt;<"Present_source_strng : "<<src_str1vàlt;<'n';<br /> src_str1.sao chép(dest_on,sizeof src_str1);<br /> coutvàlt;<"Final_string_dest:"<<dest_on;<br /> return 0;<br /> }</code></p> <p><strong>Output:</strong></p> </p> <h4>Example #2</h4> <p>This program demonstrates the copying of the string when the position of the character within the string gets passed from the parameter. It copies all the content from the source to destination and gets the value even after passing the parameter as shown in the output.</p> <p><strong>Code:</strong></p> <p><code>#includevàlt;iostreamvàgt;<br /> using namespace std;<br /> intmain()<br /> {<br /> string str = "copy_str_func";<br /> char str_p[19] ;<br /> str.sao chép(str_p,3,7);<br /> coutvàlt;<"Content_Of_String : " <<str_p;<br /> return 0;<br /> }</code></p> <p><strong>Output:</strong></p> </p> <h4>Example #3</h4> <p>This program demonstrates the strcpy() function which represents that the memory allocation for the destination string should be more so that the entire content for the source string gets copied to the destination string seamlessly as shown in the output.</p> <p><strong>Code:</strong></p> <p><code>#include <cstringvàgt;<br /> #include <iostreamvàgt;<br /> using namespace std;<br /> intmain()<br /> {<br /> char sr_1[] = "Welcome_Everyone !";<br /> char dest_n[50];<br /> strcpy(dest_n,sr_1);<br /> coutvàlt;<dest_n;<br /> return 0;<br /> }</code></p> <p><strong>Output:</strong></p> </p> <p><strong>cảnh báo</strong>: There is a major problem with strcpy() function which is like the destination string array does not specify the size of the entire array which is risky for the memory allocation as the array with large character array will not get accommodated properly and then it will create a problem for the buffer and other functioning of the array related to the memory because most often this situation arises.</p> <div style="clear:both; margin-top:0em; margin-bottom:1em;"><a href="https://phptravels.vn/cach-tao-lien-ket-mo-trong-cua-so-hoac-tab-moi-thesitewizard-com-mo-trong-tab-moi/" target="_blank" rel="dofollow" class="u93eb29a553cc0151b8c259b07d877e46"><!-- INLINE RELATED POSTS 3/3 //--><style> .u93eb29a553cc0151b8c259b07d877e46 { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:inherit; border:0!important; border-left:4px solid inherit!important; text-decoration:none; } .u93eb29a553cc0151b8c259b07d877e46:active, .u93eb29a553cc0151b8c259b07d877e46:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u93eb29a553cc0151b8c259b07d877e46 { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u93eb29a553cc0151b8c259b07d877e46 .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u93eb29a553cc0151b8c259b07d877e46 .postTitle { color:inherit; text-decoration: underline!important; font-size: 16px; } .u93eb29a553cc0151b8c259b07d877e46:hover .postTitle { text-decoration: underline!important; } </style><div style="padding-left:1em; padding-right:1em;"><span class="ctaText">Xem Thêm</span>  <span class="postTitle">Cách tạo liên kết mở trong cửa sổ hoặc tab mới (thesitewizard.com) - mở trong tab mới</span></div></a></div><h3>Conclusion</h3> <p>The strcpy() function in C++ is used mostly for copying of the content of the source string to the destination string without hampering the content of the source string and then putting all the content without changing any content in the source string. It provides flexibility and eases to the programmers.</p> <h3>Recommended Articles</h3> <p>This is a guide to C++ String Copy. Here we also discuss the definition and How to Copy String in C++? along with different examples and its code implementation. You may also have a look at the following articles to learn more –</p> <p class="nc_tweetContainer swp_share_button total_shares total_sharesalt"> <p class="swp_count">0 </p> <p class="swp_label">Shares</p> </p> <p><span class="nc_tweet swp_share_link" rel="nofollow noreferrer noopener" target="_blank"></p> <p class="swp_count swp_hide"> <p class="iconFiller"> <p class="spaceManWilly"> <p class="swp_share">Share</p> </p> </p> <p></span></p> <p class="swp-content-locator"> </div> </div> <footer class="entry-meta" aria-label="Entry meta"> <span class="cat-links"><span class="gp-icon icon-categories"><svg viewBox="0 0 512 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"><path d="M0 112c0-26.51 21.49-48 48-48h110.014a48 48 0 0143.592 27.907l12.349 26.791A16 16 0 00228.486 128H464c26.51 0 48 21.49 48 48v224c0 26.51-21.49 48-48 48H48c-26.51 0-48-21.49-48-48V112z" /></svg></span><span class="screen-reader-text">Danh mục </span><a href="https://phptravels.vn/kien-thuc-lap-trinh/" rel="category tag">Kiến thức lập trình</a></span> <nav id="nav-below" class="post-navigation" aria-label="Single Post"> <span class="screen-reader-text">Điều hướng bài viết</span> <div class="nav-previous"><span class="gp-icon icon-arrow-left"><svg viewBox="0 0 192 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M178.425 138.212c0 2.265-1.133 4.813-2.832 6.512L64.276 256.001l111.317 111.277c1.7 1.7 2.832 4.247 2.832 6.513 0 2.265-1.133 4.813-2.832 6.512L161.43 394.46c-1.7 1.7-4.249 2.832-6.514 2.832-2.266 0-4.816-1.133-6.515-2.832L16.407 262.514c-1.699-1.7-2.832-4.248-2.832-6.513 0-2.265 1.133-4.813 2.832-6.512l131.994-131.947c1.7-1.699 4.249-2.831 6.515-2.831 2.265 0 4.815 1.132 6.514 2.831l14.163 14.157c1.7 1.7 2.832 3.965 2.832 6.513z" fill-rule="nonzero" /></svg></span><span class="prev" title="Trang trước"><a href="https://phptravels.vn/khai-quat-tac-gia-nguyen-tuan/" rel="prev">Khái quát tác giả Nguyễn Tuân</a></span></div><div class="nav-next"><span class="gp-icon icon-arrow-right"><svg viewBox="0 0 192 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M178.425 256.001c0 2.266-1.133 4.815-2.832 6.515L43.599 394.509c-1.7 1.7-4.248 2.833-6.514 2.833s-4.816-1.133-6.515-2.833l-14.163-14.162c-1.699-1.7-2.832-3.966-2.832-6.515 0-2.266 1.133-4.815 2.832-6.515l111.317-111.316L16.407 144.685c-1.699-1.7-2.832-4.249-2.832-6.515s1.133-4.815 2.832-6.515l14.163-14.162c1.7-1.7 4.249-2.833 6.515-2.833s4.815 1.133 6.514 2.833l131.994 131.993c1.7 1.7 2.832 4.249 2.832 6.515z" fill-rule="nonzero" /></svg></span><span class="next" title="Tiếp"><a href="https://phptravels.vn/the-nao-la-hai-tam-giac-dong-dang-ly-thuyet-va-truong-hop-dong-dang-2021-kthn/" rel="next">Thế nào là hai tam giác đồng dạng? Lý thuyết và trường hợp đồng dạng (2021) ? KTHN ?</a></span></div> </nav> </footer> </div> </article> <div class="comments-area"> <div id="comments"> <div id="respond" class="comment-respond"> <h3 id="reply-title" class="comment-reply-title">Viết một bình luận <small><a rel="nofollow" id="cancel-comment-reply-link" href="/c-string-copy/#respond" style="display:none;">Hủy</a></small></h3><p class="must-log-in">Bạn phải <a href="https://phptravels.vn/wp-login.php?redirect_to=https%3A%2F%2Fphptravels.vn%2Fc-string-copy%2F">đăng nhập</a> để gửi phản hồi.</p> </div><!-- #respond --> </div><!-- #comments --> </div> </main> </div> <div class="widget-area sidebar is-right-sidebar" id="right-sidebar"> <div class="inside-right-sidebar"> <aside id="block-2" class="widget inner-padding widget_block widget_search"><form role="search" method="get" action="https://phptravels.vn/" class="wp-block-search__button-outside wp-block-search__text-button wp-block-search"><label for="wp-block-search__input-1" class="wp-block-search__label">Tìm kiếm</label><div class="wp-block-search__inside-wrapper " ><input type="search" id="wp-block-search__input-1" class="wp-block-search__input " name="s" value="" placeholder="" required /><button type="submit" class="wp-block-search__button " >Tìm kiếm</button></div></form></aside><aside id="block-3" class="widget inner-padding widget_block"><div class="wp-container-1 wp-block-group"><div class="wp-block-group__inner-container"><h2>Bài viết mới</h2><ul class="wp-block-latest-posts__list wp-block-latest-posts"><li><a class="wp-block-latest-posts__post-title" href="https://phptravels.vn/cach-noi-3-bang-hoac-nhieu-hon-trong-sql-tham-gia-3-bang-sql-2/">Cách nối 3 bảng (hoặc nhiều hơn) trong SQL – tham gia 3 bảng sql</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://phptravels.vn/arraylist-trong-java-mang-danh-sach-mang-java/">ArrayList trong Java – mảng danh sách mảng java</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://phptravels.vn/cac-phuong-phap-vong-doi-phan-ung-mot-huong-dan-co-the-tiep-can-voi-cac-vi-du-cac-phuong-phap-vong-doi-trong-phan-ung/">Các phương pháp vòng đời phản ứng: Một hướng dẫn có thể tiếp cận với các ví dụ – các phương pháp vòng đời trong phản ứng</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://phptravels.vn/aws-lambda-la-gi-lambda-lam-gi/">AWS Lambda là gì? – lambda làm gì</a></li> <li><a class="wp-block-latest-posts__post-title" href="https://phptravels.vn/thuoc-tinh-html-a-href-duoc-giai-thich-voi-cac-vi-du-a-href-co-nghia-la-gi-trong-html/">Thuộc tính HTML a href được giải thích với các ví dụ – a href có nghĩa là gì trong html</a></li> </ul></div></div></aside><aside id="block-4" class="widget inner-padding widget_block"><div class="wp-container-2 wp-block-group"><div class="wp-block-group__inner-container"><h2>Phản hồi gần đây</h2><div class="no-comments wp-block-latest-comments">Không có bình luận nào để hiển thị.</div></div></div></aside><aside id="block-5" class="widget inner-padding widget_block"><div class="wp-container-3 wp-block-group"><div class="wp-block-group__inner-container"><h2>Lưu trữ</h2><ul class=" wp-block-archives-list wp-block-archives"> <li><a href='https://phptravels.vn/2022/07/'>Tháng Bảy 2022</a></li> <li><a href='https://phptravels.vn/2022/06/'>Tháng Sáu 2022</a></li> <li><a href='https://phptravels.vn/2022/05/'>Tháng Năm 2022</a></li> <li><a href='https://phptravels.vn/2022/04/'>Tháng Tư 2022</a></li> <li><a href='https://phptravels.vn/2022/03/'>Tháng Ba 2022</a></li> </ul></div></div></aside><aside id="block-6" class="widget inner-padding widget_block"><div class="wp-container-4 wp-block-group"><div class="wp-block-group__inner-container"><h2>Chuyên mục</h2><ul class="wp-block-categories-list wp-block-categories"> <li class="cat-item cat-item-1"><a href="https://phptravels.vn/blog/">Blog</a> </li> <li class="cat-item cat-item-2"><a href="https://phptravels.vn/kien-thuc-lap-trinh/">Kiến thức lập trình</a> </li> <li class="cat-item cat-item-8"><a href="https://phptravels.vn/phan-mem/">phần mềm</a> </li> <li class="cat-item cat-item-3"><a href="https://phptravels.vn/thu-thuat-may-tinh/">Thủ thuật máy tính</a> </li> </ul></div></div></aside> </div> </div> </div> </div> <div class="site-footer"> <footer class="site-info" aria-label="Site" itemtype="https://schema.org/WPFooter" itemscope> <div class="inside-site-info grid-container"> <div class="copyright-bar"> <span class="copyright">© 2022 phptravels.vn</span> • Built with <a href="https://generatepress.com" itemprop="url">GeneratePress</a> </div> </div> </footer> </div> <div class='code-block code-block-3 ai-viewport-1 ai-viewport-2' style='top: 650px; position: fixed; z-index: 9995; width: 728px; height: 250px; text-align: center; left: 40%; transform: translate(-50%, -50%);opacity: 0;'> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9433987078064567" crossorigin="anonymous"></script> <!-- PhpPc3 --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-9433987078064567" data-ad-slot="6887280934" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div> <div class='code-block code-block-4 ai-viewport-3' style='top: 200px; position: fixed; z-index: 9995; width: 300px; height: 300px; text-align: center; left: 40%; transform: translate(-50%, -50%);opacity: 0;'> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9433987078064567" crossorigin="anonymous"></script> <!-- PhpMb1 --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-9433987078064567" data-ad-slot="2700623916" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div> <script id="generate-a11y">!function(){"use strict";if("querySelector"in document&&"addEventListener"in window){var e=document.body;e.addEventListener("mousedown",function(){e.classList.add("using-mouse")}),e.addEventListener("keydown",function(){e.classList.remove("using-mouse")})}}();</script><style>.wp-container-1 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-1 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-1 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style> <style>.wp-container-2 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-2 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-2 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style> <style>.wp-container-3 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-3 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-3 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style> <style>.wp-container-4 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-4 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-4 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style> <!--[if lte IE 11]> <script src='https://phptravels.vn/wp-content/themes/generatepress/assets/js/classList.min.js?ver=3.1.3' id='generate-classlist-js'></script> <![endif]--> <script id='generate-menu-js-extra'> var generatepressMenu = {"toggleOpenedSubMenus":"1","openSubMenuLabel":"Open Sub-Menu","closeSubMenuLabel":"Close Sub-Menu"}; </script> <script src='https://phptravels.vn/wp-content/themes/generatepress/assets/js/menu.min.js?ver=3.1.3' id='generate-menu-js'></script> <script src='https://phptravels.vn/wp-includes/js/comment-reply.min.js?ver=6.0.1' id='comment-reply-js'></script> <script src='https://phptravels.vn/wp-includes/js/jquery/jquery.min.js?ver=3.6.0' id='jquery-core-js'></script> <script src='https://phptravels.vn/wp-includes/js/jquery/jquery-migrate.min.js?ver=3.3.2' id='jquery-migrate-js'></script> <script id='fifu-image-js-js-extra'> var fifuImageVars = {"fifu_lazy":"","fifu_woo_lbox_enabled":"1","fifu_woo_zoom":"inline","fifu_is_product":"","fifu_is_flatsome_active":"","fifu_rest_url":"https:\/\/phptravels.vn\/wp-json\/","fifu_nonce":"32ac2535ab"}; </script> <script src='https://phptravels.vn/wp-content/plugins/featured-image-from-url/includes/html/js/image.js?ver=3.8.7' id='fifu-image-js-js'></script> </body> </html>