{"id":103,"date":"2026-05-20T11:41:52","date_gmt":"2026-05-20T11:41:52","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=103"},"modified":"2026-05-22T10:27:48","modified_gmt":"2026-05-22T10:27:48","slug":"string-handling","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=string-handling","title":{"rendered":"String Handling"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">String handling in C++ refers to working with text data such as words and sentences. C++ provides powerful ways to create, modify, compare, and manipulate strings easily.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a String?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A string is a sequence of characters used to store text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"Hello\"<br>\"Programming\"<br>\"C++ Language\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why String Handling is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">String handling is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Helps process text data<\/li>\n\n\n\n<li>Is used in user input and output<\/li>\n\n\n\n<li>Supports searching and editing text<\/li>\n\n\n\n<li>Is widely used in real-world applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">String Library in C++<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++ provides the <code>&lt;string&gt;<\/code> library for easier string handling.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Declaring a String<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>string name;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Initializing a String<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>string name = \"Ali\";<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example Program<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main() {<br><br>    string name = \"Ahmed\";<br><br>    cout &lt;&lt; name;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Taking String Input<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Single Word Input<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cin &gt;&gt; name;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Full Sentence Input<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>getline(cin, name);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of getline()<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main() {<br><br>    string sentence;<br><br>    getline(cin, sentence);<br><br>    cout &lt;&lt; sentence;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common String Functions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">length()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to find string length.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string text = \"Programming\";<br><br>cout &lt;&lt; text.length();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">append()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to join strings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string a = \"Hello \";<br>string b = \"World\";<br><br>a.append(b);<br><br>cout &lt;&lt; a;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">+ Operator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Strings can also be combined using <code>+<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string full = \"C++ \" + string(\"Programming\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">substr()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to extract part of a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string text = \"Programming\";<br><br>cout &lt;&lt; text.substr(0, 6);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">find()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to search text inside a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string text = \"Hello World\";<br><br>cout &lt;&lt; text.find(\"World\");<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Comparing Strings<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>string a = \"Ali\";<br>string b = \"Ahmed\";<br><br>if (a == b) {<br>    cout &lt;&lt; \"Same\";<br>} else {<br>    cout &lt;&lt; \"Different\";<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Loop Through String Characters<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main() {<br><br>    string text = \"Hello\";<br><br>    for (int i = 0; i &lt; text.length(); i++) {<br>        cout &lt;&lt; text&#91;i] &lt;&lt; endl;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of String Class<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy to use<\/li>\n\n\n\n<li>Dynamic size<\/li>\n\n\n\n<li>Built-in functions available<\/li>\n\n\n\n<li>Safer than character arrays<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between String and Character Array<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>String Class<\/th><th>Character Array<\/th><\/tr><\/thead><tbody><tr><td>Dynamic size<\/td><td>Fixed size<\/td><\/tr><tr><td>Easier handling<\/td><td>Manual handling<\/td><\/tr><tr><td>Built-in functions<\/td><td>Limited functions<\/td><\/tr><tr><td>Safer<\/td><td>Less safe<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Why String Handling is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">String handling is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Helps process user data<\/li>\n\n\n\n<li>Is used in text-based applications<\/li>\n\n\n\n<li>Supports file and web development<\/li>\n\n\n\n<li>Simplifies programming tasks<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a text editor:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You write text<\/li>\n\n\n\n<li>Edit words<\/li>\n\n\n\n<li>Search sentences<\/li>\n\n\n\n<li>Combine paragraphs<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">All these operations use string handling.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">String handling in C++ allows programmers to efficiently work with text data using powerful built-in functions and the string class. It is an essential concept for creating interactive and real-world applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Arrays and Strings > String Handling<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":28,"template":"","class_list":["post-103","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>String Handling - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ string handling with examples of string functions, input\/output, concatenation, and text processing using the string library.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"String Handling - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ string handling with examples of string functions, input\/output, concatenation, and text processing using the string library.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T10:27:48+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=string-handling\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"String Handling - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T11:41:52+00:00\",\"dateModified\":\"2026-05-22T10:27:48+00:00\",\"description\":\"Learn C++ string handling with examples of string functions, input\\\/output, concatenation, and text processing using the string library.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Arrays and Strings > String Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"String Handling - Learn C++Language with GiGz.PK","description":"Learn C++ string handling with examples of string functions, input\/output, concatenation, and text processing using the string library.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"String Handling - Learn C++Language with GiGz.PK","og_description":"Learn C++ string handling with examples of string functions, input\/output, concatenation, and text processing using the string library.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T10:27:48+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=string-handling","url":"https:\/\/gigz.pk\/","name":"String Handling - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T11:41:52+00:00","dateModified":"2026-05-22T10:27:48+00:00","description":"Learn C++ string handling with examples of string functions, input\/output, concatenation, and text processing using the string library.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Arrays and Strings > String Handling"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}