{"id":209,"date":"2020-08-31T08:13:26","date_gmt":"2020-08-31T08:13:26","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=209"},"modified":"2020-08-31T08:13:26","modified_gmt":"2020-08-31T08:13:26","slug":"c-string-manipulation","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-string-manipulation\/","title":{"rendered":"C++ String Manipulation"},"content":{"rendered":"<p>In this lesson, you will learn about string manipulation in C++. We&#8217;ll cover all the various operations that you can do with strings.<\/p>\n<ol>\n<li><a href=\"#t1\">C-Type Strings<\/a><\/li>\n<li><a href=\"#t2\">C++ String Object<\/a><\/li>\n<li><a href=\"#t3\">String Manipulation Functions from C<\/a><\/li>\n<li><a href=\"#t4\">String Manipulation with C++ String Class<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. C-Type Strings<\/strong><\/h4>\n<p>As you know, C++ supersets the C language. Therefore, it supports string functionalities from C. This is also called C-String. Strings in C are <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-arrays\/#t2\" target=\"_blank\" rel=\"noopener noreferrer\">1-dimensional array<\/a> of characters that is terminated by a null character, &#8216;\\0&#8217; (ASCII code for 0).<\/p>\n<p>To define a C-string in C++ use the code below;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">char<\/span> name[] <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>;\r\n<\/pre>\n<p>In the code above, the string name is created and holds 7 characters.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">char<\/span> str[<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>] <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>;\r\n\t \r\n<span style=\"color: #333399; font-weight: bold;\">char<\/span> str[] <span style=\"color: #333333;\">=<\/span> {<span style=\"color: #0044dd;\">'K'<\/span>,<span style=\"color: #0044dd;\">'i'<\/span>,<span style=\"color: #0044dd;\">'n'<\/span>, <span style=\"color: #0044dd;\">'d'<\/span>, <span style=\"color: #0044dd;\">'s'<\/span>, <span style=\"color: #0044dd;\">'o'<\/span>, <span style=\"color: #0044dd;\">'n'<\/span>, <span style=\"color: #0044dd;\">'\\0'<\/span>};\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">char<\/span> str[<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>] <span style=\"color: #333333;\">=<\/span> {<span style=\"color: #0044dd;\">'H'<\/span>,<span style=\"color: #0044dd;\">'e'<\/span>,<span style=\"color: #0044dd;\">'l'<\/span>, <span style=\"color: #0044dd;\">'l'<\/span>, <span style=\"color: #0044dd;\">'o'<\/span>, <span style=\"color: #0044dd;\">'\\0'<\/span>};\r\n<\/pre>\n<p>The program below uses a C-type string to read a name from the input. Then displays the name to the output.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\t<span style=\"color: #888888;\">\/\/you must provide a size<\/span>\r\n\t<span style=\"color: #333399; font-weight: bold;\">char<\/span> name[<span style=\"color: #0000dd; font-weight: bold;\">20<\/span>];\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Please enter your name: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> name;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Hello \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> name <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. C++ String Object<\/strong><\/h4>\n<p>C++ also provides it&#8217;s own string class. So you can create string object. This class is provided by the standard C++ library. Unlike character array from C, the C++ string does not have a fixed length.\u00a0 For example, the code below, reads and prints the user&#8217;s name but without knowing the size of the name:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Example with C++ String<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\t<span style=\"color: #888888;\">\/\/ size is not needed<\/span>\r\n\tstring name;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"What is your name: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> name;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Welcome \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> name <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. String Manipulation Methods (from C)<\/strong><\/h4>\n<p>These are methods used for string manipulation. You need to understand these functions to be get things done with strings.<\/p>\n<p>See table below:<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN<\/th>\n<th>Method and description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>strcpy(string1, string2)<\/b><\/p>\n<p>Copies string string2 into string string1.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>strcat(string1, string2)<\/b><\/p>\n<p>Concatenates string string2 onto the end of string string1.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>strlen(string1)<\/b><\/p>\n<p>Returns the length of string string1.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>strcmp(string1, string2)<\/b><\/p>\n<p>Returns 0 if string1 and string2 are the same; less than 0 if string1&lt;string2; greater than 0 if string1 &gt; string2.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>strchr(string1, ch);<\/b><\/p>\n<p>Returns a pointer to the first occurrence of character ch in string string1.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>strstr(s1, s2)<\/b><\/p>\n<p>Returns a pointer to the first occurrence of string string2 in string string1.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The functions above derives from C. Therefore to use them, you need to include &lt;cstring&gt; header in your program.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. String Manipulation With C++ String Class<\/strong><\/h4>\n<p>Let&#8217;s now see how to do the same thing using C++ string class<\/p>\n<ul>\n<li><strong>Concatenation<\/strong> &#8211; Combines two string into one. Simply use the <em><strong>+ operator<\/strong><\/em>. See example below<\/li>\n<li><strong>String length<\/strong> &#8211; Use<em><strong> length()<\/strong><\/em> method<\/li>\n<li><strong>Searching strings<\/strong> &#8211; Accessing a character within a string. Use the <strong>find()<\/strong> method<\/li>\n<li><strong>Substrings<\/strong> &#8211; Returning part of a string. Use the <strong><em>substr()<\/em><\/strong> method.<\/li>\n<li><strong>Replacing<\/strong> &#8211; Replacing part of a string. Use the<strong><em> replace()<\/em><\/strong> method<\/li>\n<li><strong>Insertion<\/strong> &#8211; Inserting character(s) into a string. Using the<em><strong> insert()<\/strong><\/em> method<\/li>\n<li><strong>Erase<\/strong> &#8211; Removing part of a string. Use the<strong><em> erase()<\/em><\/strong> method.<\/li>\n<\/ul>\n<p>The program below shows how these methods can be applied.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ String manipulation using methods<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\t   string string1 <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Beginner \"<\/span>;\r\n\t   string string2 <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"to Expert \"<\/span>;\r\n\t   string string3 <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Tutorials\"<\/span>;\r\n\t   string string4 <span style=\"color: #333333;\">=<\/span> string1 <span style=\"color: #333333;\">+<\/span> string2 <span style=\"color: #333333;\">+<\/span> string3;\r\n\t   <span style=\"color: #333399; font-weight: bold;\">int<\/span>  len <span style=\"color: #333333;\">=<\/span> string4.length();\r\n\r\n\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> string4 <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Length of string1 is: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> len <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Expert is at position \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> string2.find(<span style=\"background-color: #fff0f0;\">\"Expert\"<\/span>) <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Part of string 2: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> string2.substr(<span style=\"color: #0000dd; font-weight: bold;\">3<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">8<\/span>)<span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Replacing 'Expert':  \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> string4.replace(<span style=\"color: #0000dd; font-weight: bold;\">12<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">17<\/span>, <span style=\"background-color: #fff0f0;\">\"Guru\"<\/span>)<span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Insertion:  \"<\/span><span style=\"color: #333333;\">&lt;&lt;<\/span> string4.insert(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"background-color: #fff0f0;\">\" by Kindson\"<\/span>)<span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n\t   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Erasing:  \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> string3.erase(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">3<\/span>)<span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n\t   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>If you successfully run the program, you will have the output:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Beginner to Expert Tutorials\r\nLength of string1 is: 28\r\nExpert is at position3\r\nPart of string 2: Expert \r\nReplacing 'Expert':  Beginner to Guru\r\nInsertion:   by KindsonBeginner to Guru\r\nErasing:  orials\r\n<\/pre>\n<p>I would recommend you take some time to understand how it works to produce the outputs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, you will learn about string manipulation in C++. We&#8217;ll cover all the various operations that you can do with strings. C-Type Strings &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-c-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":210,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/209\/revisions\/210"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}