Strings in C++: Character Sequences, std::string, and Common Operations
What is Strings in C++: Character Sequences, std::string, and Common Operations? Strings are an important part of almost every programming language. They allow programs to store and process text such as names, addresses, messages, email addr...
How does std::string manage dynamic memory, Small String Optimization (SSO), and string manipulation in C++?
Explanation & In-Depth Details
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">name</span> <span class="js-op">=</span> <span class="js-str">"Admin"</span><span class="js-op">;</span></code></pre></div><p>Here, <code>name</code> stores the text <code>"<span style="color: rgb(74, 222, 128); font-family: "Fira Code", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; white-space: pre; background-color: rgb(15, 23, 42);">Admin</span>"</code>.</p><p>Strings can contain:</p><ul><li><p>Alphabetic characters: <code>"Hello"</code></p></li><li><p>Numeric characters: <code>"12345"</code></p></li><li><p>Special characters: <code>"@#$%"</code></p></li><li><p>Spaces: <code>"Welcome to C++"</code></p></li><li><p>Mixed characters: <code>"User123"</code></p></li></ul><p>C++ provides two common ways to represent strings:</p><ol start="1"><li><p>C-style strings using character arrays.</p></li><li><p>C++ strings using the <code>std::string</code> class.</p></li></ol><h2>2. C-Style Strings in C++</h2><p>A C-style string is a character array that ends with a special null character, written as <code>'\0'</code>.</p><p>The null character marks the end of the string.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">char</span> <span class="js-id">string_name</span>[<span class="js-id">size</span>]<span class="js-op">;</span></code></pre></div><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">char</span> <span class="js-id">name</span>[<span class="js-num">10</span>] <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span></code></pre></div><p>The array stores the characters:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">H</span> <span class="js-id">e</span> <span class="js-id">l</span> <span class="js-id">l</span> <span class="js-id">o</span> \<span class="js-num">0</span></code></pre></div><p>The null character occupies one additional array position.</p><h3>Example: Declare and Display a C-Style String</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">char</span> <span class="js-id">name</span>[] <span class="js-op">=</span> <span class="js-str">"Hello C++"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Hello</span> <span class="js-id">C</span><span class="js-op">++</span></code></pre></div><p>The compiler automatically calculates the required array size, including space for the terminating null character.</p><h3>Important: Character Array vs Character</h3><p>A single character uses the <code>char</code> data type:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">char</span> <span class="js-id">letter</span> <span class="js-op">=</span> <span class="js-str">'A'</span><span class="js-op">;</span></code></pre></div><p>A C-style string uses a character array:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">char</span> <span class="js-id">word</span>[] <span class="js-op">=</span> <span class="js-str">"Apple"</span><span class="js-op">;</span></code></pre></div><p>Use single quotation marks for a character and double quotation marks for a string literal.</p><h2>3. What Is std::string in C++?</h2><p><code>std::string</code> is a standard C++ library class used to store and manipulate text.</p><p>It is available through the <code><string></code> header.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">std</span><span class="js-op">::</span><span class="js-id">string</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">"Welcome to C++"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">message</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Welcome</span> <span class="js-id">to</span> <span class="js-id">C</span><span class="js-op">++</span></code></pre></div><p>Unlike a basic character array, a <code>std::string</code> can automatically manage its storage as the text changes.</p><h3>Advantages of std::string</h3><ul><li><p>Easy declaration and initialization.</p></li><li><p>Automatically manages its character storage.</p></li><li><p>Supports concatenation using <code>+</code>.</p></li><li><p>Provides built-in functions for searching and modifying text.</p></li><li><p>Supports comparison using operators.</p></li><li><p>Can grow or shrink as needed.</p></li></ul><p>For most general-purpose C++ programming, <code>std::string</code> is the recommended way to work with text.</p><h2>4. Declaring and Initializing Strings</h2><p>There are several ways to declare and initialize a string in C++.</p><h3>Example 1: Declare an Empty String</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Enter your name: "</span><span class="js-op">;</span>
<span class="js-id">cin</span> <span class="js-op">>></span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Your name is "</span> <span class="js-op"><<</span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Sample input:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Tanmoy</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Enter</span> <span class="js-id">your</span> <span class="js-id">name</span><span class="js-op">:</span> <span class="js-id">Your</span> <span class="js-id">name</span> <span class="js-id">is</span> <span class="js-id">Tanmoy</span></code></pre></div><h3>Example 2: Initialize a String Directly</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">city</span> <span class="js-op">=</span> <span class="js-str">"Kolkata"</span><span class="js-op">;</span></code></pre></div><h3>Example 3: Initialize Using Another String</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">first</span> <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">second</span> <span class="js-op">=</span> <span class="js-id">first</span><span class="js-op">;</span></code></pre></div><p>The value of <code>first</code> is copied into <code>second</code>.</p><h3>Example 4: Initialize an Empty String</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">""</span><span class="js-op">;</span></code></pre></div><p>This creates an empty string.</p><h3>Example 5: Use String Assignment</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">language</span><span class="js-op">;</span>
<span class="js-id">language</span> <span class="js-op">=</span> <span class="js-str">"C++"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">language</span><span class="js-op">;</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">C</span><span class="js-op">++</span></code></pre></div><h2>5. Taking String Input in C++</h2><p>C++ provides <code>cin</code> and <code>getline()</code> to read string input.</p><p>The choice depends on whether you want to read one word or a complete line.</p><h3>5.1 Using cin to Read a Single Word</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Enter your name: "</span><span class="js-op">;</span>
<span class="js-id">cin</span> <span class="js-op">>></span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Hello, "</span> <span class="js-op"><<</span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Sample input:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Tanmoy</span> <span class="js-id">Maity</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Enter</span> <span class="js-id">your</span> <span class="js-id">name</span><span class="js-op">:</span> <span class="js-id">Hello</span><span class="js-op">,</span> <span class="js-id">Tanmoy</span></code></pre></div><p><strong>Why does this happen?</strong></p><p>The extraction operator <code>>></code> stops reading at whitespace. Therefore, it reads only the first word.</p><h3>5.2 Using getline() to Read a Complete Line</h3><p>Use <code>getline()</code> when you want to read a sentence or text containing spaces.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">fullName</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Enter your full name: "</span><span class="js-op">;</span>
<span class="js-id">getline</span><span class="js-op">(</span><span class="js-id">cin</span><span class="js-op">,</span> <span class="js-id">fullName</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Your name is: "</span> <span class="js-op"><<</span> <span class="js-id">fullName</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Sample input:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Tanmoy</span> <span class="js-id">Maity</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Enter</span> <span class="js-id">your</span> <span class="js-id">full</span> <span class="js-id">name</span><span class="js-op">:</span> <span class="js-id">Your</span> <span class="js-id">name</span> <span class="js-id">is</span><span class="js-op">:</span> <span class="js-id">Tanmoy</span> <span class="js-id">Maity</span></code></pre></div><p>The <code>getline()</code> function reads characters until it encounters a newline.</p><h3>5.3 Using getline() After cin</h3><p>A common beginner mistake happens when <code>getline()</code> is used immediately after <code>cin >></code>.</p><p>Example of the problem:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">age</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-id">cin</span> <span class="js-op">>></span> <span class="js-id">age</span><span class="js-op">;</span>
<span class="js-id">getline</span><span class="js-op">(</span><span class="js-id">cin</span><span class="js-op">,</span> <span class="js-id">name</span><span class="js-op">);</span></code></pre></div><p>After reading the number, the newline character remains in the input stream. The next <code>getline()</code> may consume that newline and return an empty string.</p><p>Use <code>cin.ignore()</code> to discard the remaining characters through the next newline.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">limits</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">age</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Enter your age: "</span><span class="js-op">;</span>
<span class="js-id">cin</span> <span class="js-op">>></span> <span class="js-id">age</span><span class="js-op">;</span>
<span class="js-id">cin</span><span class="js-op">.</span><span class="js-id">ignore</span><span class="js-op">(</span><span class="js-id">numeric_limits</span><span class="js-op"><</span><span class="js-id">streamsize</span><span class="js-op">>::</span><span class="js-id">max</span><span class="js-op">(),</span> <span class="js-str">'\n'</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Enter your full name: "</span><span class="js-op">;</span>
<span class="js-id">getline</span><span class="js-op">(</span><span class="js-id">cin</span><span class="js-op">,</span> <span class="js-id">name</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Age: "</span> <span class="js-op"><<</span> <span class="js-id">age</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Name: "</span> <span class="js-op"><<</span> <span class="js-id">name</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Sample input:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">35</span>
<span class="js-id">Tanmoy</span> <span class="js-id">Maity</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Enter</span> <span class="js-id">your</span> <span class="js-id">age</span><span class="js-op">:</span> <span class="js-id">Enter</span> <span class="js-id">your</span> <span class="js-id">full</span> <span class="js-id">name</span><span class="js-op">:</span> <span class="js-id">Age</span><span class="js-op">:</span> <span class="js-num">35</span>
<span class="js-id">Name</span><span class="js-op">:</span> <span class="js-id">Tanmoy</span> <span class="js-id">Maity</span></code></pre></div><p>This is a reliable approach when reading a number followed by a complete line of text.</p><h2>6. Finding the Length of a String</h2><p>The <code>length()</code> and <code>size()</code> functions return the number of characters stored in a <code>std::string</code>.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">"Hello C++"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Length: "</span> <span class="js-op"><<</span> <span class="js-id">message</span><span class="js-op">.</span><span class="js-id">length</span><span class="js-op">()</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Size: "</span> <span class="js-op"><<</span> <span class="js-id">message</span><span class="js-op">.</span><span class="js-id">size</span><span class="js-op">();</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Length</span><span class="js-op">:</span> <span class="js-num">9</span>
<span class="js-id">Size</span><span class="js-op">:</span> <span class="js-num">9</span></code></pre></div><p>Both functions return the same value for <code>std::string</code>.</p><p>Spaces are included in the character count.</p><p><strong>Note:</strong> For UTF-8 text, the returned size counts bytes, not necessarily user-perceived characters. A visible character may occupy multiple bytes.</p><h2>7. Accessing Individual Characters in a String</h2><p>You can access individual characters using an index.</p><p>String indexing starts at zero.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">word</span> <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">word</span>[<span class="js-num">0</span>] <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">word</span>[<span class="js-num">1</span>] <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">word</span>[<span class="js-num">2</span>] <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">H</span>
<span class="js-id">e</span>
<span class="js-id">l</span></code></pre></div><p>You can also use the <code>at()</code> function.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">word</span> <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">word</span><span class="js-op">.</span><span class="js-id">at</span><span class="js-op">(</span><span class="js-num">0</span><span class="js-op">);</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">H</span></code></pre></div><p>The difference is that <code>at()</code> performs bounds checking and throws <code>std::out_of_range</code> when the index is invalid. Using an invalid index with <code>operator[]</code> is not safe.</p><h2>8. Updating Characters in a String</h2><p>You can modify a character by assigning a new value to a valid index.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">word</span> <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span>
<span class="js-id">word</span>[<span class="js-num">0</span>] <span class="js-op">=</span> <span class="js-str">'Y'</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">word</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Yello</span></code></pre></div><p>The first character has been changed from <code>H</code> to <code>Y</code>.</p><h2>9. Joining Strings Using Concatenation</h2><p>Concatenation means joining two or more strings together.</p><p>C++ supports concatenation using the <code>+</code> and <code>+=</code> operators.</p><h3>Example 1: Using the + Operator</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">first</span> <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">second</span> <span class="js-op">=</span> <span class="js-str">"World"</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">first</span> <span class="js-op">+</span> <span class="js-str">" "</span> <span class="js-op">+</span> <span class="js-id">second</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">result</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Hello</span> <span class="js-id">World</span></code></pre></div><h3>Example 2: Using the += Operator</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span>
<span class="js-id">message</span> <span class="js-op">+=</span> <span class="js-str">" C++"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">message</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Hello</span> <span class="js-id">C</span><span class="js-op">++</span></code></pre></div><p>The <code>+=</code> operator appends additional text to the existing string.</p><h2>10. Comparing Strings in C++</h2><p>You can compare two <code>std::string</code> objects using comparison operators.</p><p>Common operators include:</p><table><tbody><tr><th>Operator</th><th>Meaning</th></tr><tr><td><code>==</code></td><td>Equal to</td></tr><tr><td><code>!=</code></td><td>Not equal to</td></tr><tr><td><code><</code></td><td>Lexicographically less than</td></tr><tr><td><code>></code></td><td>Lexicographically greater than</td></tr><tr><td><code><=</code></td><td>Less than or equal to</td></tr><tr><td><code>>=</code></td><td>Greater than or equal to</td></tr></tbody></table><h3>Example: Compare Two Strings</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">first</span> <span class="js-op">=</span> <span class="js-str">"Apple"</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">second</span> <span class="js-op">=</span> <span class="js-str">"Apple"</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">first</span> <span class="js-op">==</span> <span class="js-id">second</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Strings are equal"</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Strings are different"</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Strings</span> <span class="js-id">are</span> <span class="js-id">equal</span></code></pre></div><p>String comparisons are case-sensitive.</p><p>For example, <code>"Apple"</code> and <code>"apple"</code> are different strings.</p><p>The relational operators use lexicographical comparison based on character values. They do not perform dictionary-style language-aware sorting.</p><h2>11. Searching for a Character or Word in a String</h2><p>The <code>find()</code> function searches for a character or substring.</p><p>It returns the position of the first match. If no match is found, it returns <code>string::npos</code>.</p><h3>Example: Search for a Word</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">"Welcome to C++ programming"</span><span class="js-op">;</span>
<span class="js-id">size_t</span> <span class="js-id">position</span> <span class="js-op">=</span> <span class="js-id">message</span><span class="js-op">.</span><span class="js-id">find</span><span class="js-op">(</span><span class="js-str">"C++"</span><span class="js-op">);</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">position</span> <span class="js-op">!=</span> <span class="js-id">string</span><span class="js-op">::</span><span class="js-id">npos</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Found at position: "</span> <span class="js-op"><<</span> <span class="js-id">position</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Not found"</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Found</span> <span class="js-id">at</span> <span class="js-id">position</span><span class="js-op">:</span> <span class="js-num">11</span></code></pre></div><p>Explanation:</p><ul><li><p><code>find("C++")</code> searches for the substring.</p></li><li><p><code>position</code> stores the starting index.</p></li><li><p><code>string::npos</code> indicates that the search did not find a match.</p></li></ul><h2>12. Extracting a Substring</h2><p>The <code>substr()</code> function extracts part of a string.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string_name</span><span class="js-op">.</span><span class="js-id">substr</span><span class="js-op">(</span><span class="js-id">start_position</span><span class="js-op">,</span> <span class="js-id">length</span><span class="js-op">);</span></code></pre></div><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">"Hello World"</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">message</span><span class="js-op">.</span><span class="js-id">substr</span><span class="js-op">(</span><span class="js-num">0</span><span class="js-op">,</span> <span class="js-num">5</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">result</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Hello</span></code></pre></div><p>Here:</p><ul><li><p><code>0</code> is the starting position.</p></li><li><p><code>5</code> is the number of characters to extract.</p></li></ul><p>If the requested length extends beyond the end of the string, <code>substr()</code> returns the available remaining characters.</p><h2>13. Inserting and Removing Characters</h2><p>C++ provides <code>insert()</code> and <code>erase()</code> to modify strings.</p><h3>13.1 Using insert()</h3><p>The <code>insert()</code> function adds characters at a specified position.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">"Hello World"</span><span class="js-op">;</span>
<span class="js-id">message</span><span class="js-op">.</span><span class="js-id">insert</span><span class="js-op">(</span><span class="js-num">6</span><span class="js-op">,</span> <span class="js-str">"C++ "</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">message</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Hello</span> <span class="js-id">C</span><span class="js-op">++</span> <span class="js-id">World</span></code></pre></div><h3>13.2 Using erase()</h3><p>The <code>erase()</code> function removes characters from a string.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">"Hello C++ World"</span><span class="js-op">;</span>
<span class="js-id">message</span><span class="js-op">.</span><span class="js-id">erase</span><span class="js-op">(</span><span class="js-num">6</span><span class="js-op">,</span> <span class="js-num">4</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">message</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Hello</span> <span class="js-id">World</span></code></pre></div><p>The first argument specifies the starting position, and the second specifies the number of characters to remove.</p><h2>14. Replacing Part of a String</h2><p>The <code>replace()</code> function replaces a selected portion of a string with new text.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">message</span> <span class="js-op">=</span> <span class="js-str">"I like Java"</span><span class="js-op">;</span>
<span class="js-id">message</span><span class="js-op">.</span><span class="js-id">replace</span><span class="js-op">(</span><span class="js-num">7</span><span class="js-op">,</span> <span class="js-num">4</span><span class="js-op">,</span> <span class="js-str">"C++"</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">message</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">I</span> <span class="js-id">like</span> <span class="js-id">C</span><span class="js-op">++</span></code></pre></div><p>Here, four characters starting at index 7 are replaced with <code>"C++"</code>.</p><h2>15. Converting Numbers and Strings</h2><p>C++ provides functions to convert between numeric values and strings.</p><p>The <code><string></code> header provides commonly used conversion functions.</p><h3>15.1 Convert a Number to a String</h3><p>Use <code>to_string()</code>.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">to_string</span><span class="js-op">(</span><span class="js-id">number</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">result</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">100</span></code></pre></div><p>The result is a string, not an integer.</p><h3>15.2 Convert a String to an Integer</h3><p>Use <code>stoi()</code>.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">string</span> <span class="js-id">text</span> <span class="js-op">=</span> <span class="js-str">"250"</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-id">stoi</span><span class="js-op">(</span><span class="js-id">text</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op">+</span> <span class="js-num">50</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Output:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">300</span></code></pre></div><p>Other useful conversion functions include:</p><table><tbody><tr><th>Function</th><th>Purpose</th></tr><tr><td><code>stoi()</code></td><td>Convert to <code>int</code></td></tr><tr><td><code>stol()</code></td><td>Convert to <code>long</code></td></tr><tr><td><code>stoll()</code></td><td>Convert to <code>long long</code></td></tr><tr><td><code>stof()</code></td><td>Convert to <code>float</code></td></tr><tr><td><code>stod()</code></td><td>Convert to <code>double</code></td></tr><tr><td><code>to_string()</code></td><td>Convert a numeric value to a string</td></tr></tbody></table><p>These conversion functions can throw exceptions when the input is invalid or the value is outside the target type's range. For input validation, handle exceptions or consider alternatives such as <code>std::from_chars</code> where appropriate.</p><h2>16. Useful String Functions in C++</h2><p>The following table summarizes commonly used <code>std::string</code> operations.</p><table><tbody><tr><th>Function or operator</th><th>Purpose</th><th>Example</th></tr><tr><td><code>length()</code></td><td>Returns string length</td><td><code>text.length()</code></td></tr><tr><td><code>size()</code></td><td>Returns number of characters stored</td><td><code>text.size()</code></td></tr><tr><td><code>empty()</code></td><td>Checks whether string is empty</td><td><code>text.empty()</code></td></tr><tr><td><code>clear()</code></td><td>Removes all characters</td><td><code>text.clear()</code></td></tr><tr><td><code>append()</code></td><td>Adds text to the end</td><td><code>text.append("Hi")</code></td></tr><tr><td><code>push_back()</code></td><td>Adds one character</td><td><code>text.push_back('A')</code></td></tr><tr><td><code>pop_back()</code></td><td>Removes the last character</td><td><code>text.pop_back()</code></td></tr><tr><td><code>find()</code></td><td>Searches for text</td><td><code>text.find("C++")</code></td></tr><tr><td><code>substr()</code></td><td>Extracts part of a string</td><td><code>text.substr(0, 3)</code></td></tr><tr><td><code>insert()</code></td><td>Inserts characters</td><td><code>text.insert(0, "Hi ")</code></td></tr><tr><td><code>erase()</code></td><td>Removes characters</td><td><code>text.erase(0, 2)</code></td></tr><tr><td><code>replace()</code></td><td>Replaces characters</td><td><code>text.replace(0, 2, "Hi")</code></td></tr><tr><td><code>compare()</code></td><td>Compares strings</td><td><code>text.compare("Hello")</code></td></tr><tr><td><code>c_str()</code></td><td>Provides a null-terminated character representation</td><td><code>text.c_str()</code></td></tr></tbody></table><h2>17. C-Style Strings vs std::string</h2><p>Both approaches can store text, but they work differently.</p><table><tbody><tr><th>Feature</th><th>C-Style String</th><th>std::string</th></tr><tr><td>Representation</td><td>Character array</td><td>Standard library class</td></tr><tr><td>Header</td><td>No special header for the type</td><td><code><string></code></td></tr><tr><td>Ending</td><td>Uses <code>'\0'</code></td><td>Manages its own length</td></tr><tr><td>Size management</td><td>Programmer manages array capacity</td><td>Manages storage automatically</td></tr><tr><td>Concatenation</td><td>Functions such as <code>strcat()</code></td><td><code>+</code> or <code>+=</code></td></tr><tr><td>Comparison</td><td>Functions such as <code>strcmp()</code></td><td><code>==</code>, <code>!=</code>, and other operators</td></tr><tr><td>Searching</td><td>Functions such as <code>strstr()</code></td><td><code>find()</code></td></tr><tr><td>Ease of use</td><td>Requires more manual handling</td><td>Generally easier</td></tr></tbody></table><p>For most modern C++ programs, prefer <code>std::string</code>. C-style strings remain useful when interacting with C libraries or APIs that require null-terminated character arrays.</p><h2>18. Common Mistakes When Working With Strings</h2><h3>Mistake 1: Using cin When You Need a Complete Sentence</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">message</span><span class="js-op">;</span>
<span class="js-id">cin</span> <span class="js-op">>></span> <span class="js-id">message</span><span class="js-op">;</span></code></pre></div><p>This reads only one word.</p><p>Use:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">getline</span><span class="js-op">(</span><span class="js-id">cin</span><span class="js-op">,</span> <span class="js-id">message</span><span class="js-op">);</span></code></pre></div><p>when you need to read a complete line.</p><h3>Mistake 2: Forgetting the Null Character in a C-Style String</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">char</span> <span class="js-id">name</span>[<span class="js-num">5</span>] <span class="js-op">=</span> <span class="js-op">{</span>
<span class="js-str">'H'</span><span class="js-op">,</span> <span class="js-str">'e'</span><span class="js-op">,</span> <span class="js-str">'l'</span><span class="js-op">,</span> <span class="js-str">'l'</span><span class="js-op">,</span> <span class="js-str">'o'</span>
<span class="js-op">}</span>
<span class="js-op">;</span></code></pre></div><p>This array does not contain a terminating null character. It is not a valid null-terminated C-style string.</p><p>Use:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">char</span> <span class="js-id">name</span>[<span class="js-num">6</span>] <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span></code></pre></div><h3>Mistake 3: Accessing a Character Outside the String</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">word</span> <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">word</span>[<span class="js-num">10</span>]<span class="js-op">;</span></code></pre></div><p>The index is outside the valid character range. Check the string length before accessing a character.</p><h3>Mistake 4: Confusing Character and String Literals</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">char</span> <span class="js-id">letter</span> <span class="js-op">=</span> <span class="js-str">"A"</span><span class="js-op">;</span> <span class="js-cm">// Incorrect</span></code></pre></div><p>Use:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">char</span> <span class="js-id">letter</span> <span class="js-op">=</span> <span class="js-str">'A'</span><span class="js-op">;</span></code></pre></div><p>A character uses single quotation marks. A string literal uses double quotation marks.</p><h3>Mistake 5: Comparing Strings Incorrectly</h3><p>For <code>std::string</code>, use:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">first</span> <span class="js-op">==</span> <span class="js-id">second</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Equal"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Do not use pointer comparison when you intend to compare the contents of C-style strings. Use <code>strcmp()</code> from <code><cstring></code> for that purpose.</p><h3>Mistake 6: Ignoring Case Sensitivity</h3><p>The strings <code>"Hello"</code> and <code>"hello"</code> are not equal using the ordinary comparison operators.</p><p>If you need case-insensitive matching, use an appropriate character-processing approach or a Unicode-aware library when handling international text.</p><h2>19. Best Practices for Working With Strings in C++</h2><ol start="1"><li><p>Prefer <code>std::string</code> for general text processing.</p></li><li><p>Include the <code><string></code> header when using <code>std::string</code>.</p></li><li><p>Use <code>getline()</code> to read complete lines.</p></li><li><p>Use <code>const std::string&</code> when a function only needs to read a string and you want to avoid copying it.</p></li><li><p>Check indexes before accessing characters.</p></li><li><p>Use <code>find()</code> and check for <code>string::npos</code> when searching.</p></li><li><p>Use <code>empty()</code> to check whether a string contains no characters.</p></li><li><p>Be careful when mixing <code>cin >></code> and <code>getline()</code>.</p></li><li><p>Handle exceptions when using numeric conversion functions.</p></li><li><p>Use Unicode-aware tools when processing multilingual text that requires character-level operations.</p></li></ol><h2>20. Frequently Asked Questions (FAQs)</h2><h3>1. What is a string in C++?</h3><p>A string is a sequence of characters used to represent text. C++ supports strings through character arrays and the <code>std::string</code> class.</p><h3>2. What is std::string in C++?</h3><p><code>std::string</code> is a standard library class that provides convenient ways to store, access, compare, search, and modify text.</p><h3>3. Which header file is required for strings in C++?</h3><p>Include the <code><string></code> header to use <code>std::string</code>.</p><h3>4. What is the difference between cin and getline()?</h3><p><code>cin >></code> normally reads input until whitespace. <code>getline()</code> reads a complete line, including spaces, until it encounters a newline.</p><h3>5. How do you find the length of a string?</h3><p>Use <code>length()</code> or <code>size()</code>:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">name</span> <span class="js-op">=</span> <span class="js-str">"Tanmoy"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">name</span><span class="js-op">.</span><span class="js-id">length</span><span class="js-op">();</span></code></pre></div><h3>6. How do you join two strings in C++?</h3><p>Use the <code>+</code> or <code>+=</code> operator:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-str">"Hello"</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-str">"World"</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-str">" "</span> <span class="js-op">+</span> <span class="js-id">b</span><span class="js-op">;</span></code></pre></div><h3>7. How do you compare two strings?</h3><p>Use the comparison operators:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">first</span> <span class="js-op">==</span> <span class="js-id">second</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Equal"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><h3>8. How do you search for a word inside a string?</h3><p>Use the <code>find()</code> function:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">string</span> <span class="js-id">text</span> <span class="js-op">=</span> <span class="js-str">"Learn C++"</span><span class="js-op">;</span>
<span class="js-id">size_t</span> <span class="js-id">position</span> <span class="js-op">=</span> <span class="js-id">text</span><span class="js-op">.</span><span class="js-id">find</span><span class="js-op">(</span><span class="js-str">"C++"</span><span class="js-op">);</span></code></pre></div><p>Check whether the result equals <code>string::npos</code> to determine if the text was found.</p><h3>9. Can a C++ string contain spaces?</h3><p>Yes. A <code>std::string</code> can contain spaces, but you should use <code>getline()</code> when reading an entire line from standard input.</p><h3>10. What is the difference between a C-style string and std::string?</h3><p>A C-style string is a character array terminated by <code>'\0'</code>. <code>std::string</code> is a standard library class that manages its storage and provides built-in text operations.</p><h3>11. Can we convert an integer into a string?</h3><p>Yes. Use <code>to_string()</code>:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">string</span> <span class="js-id">text</span> <span class="js-op">=</span> <span class="js-id">to_string</span><span class="js-op">(</span><span class="js-id">number</span><span class="js-op">);</span></code></pre></div><h3>12. Is std::string mutable in C++?</h3><p>Yes. You can modify its characters, append text, insert characters, erase sections, and replace content.</p>
Code Example
#include <iostream>
#include <string>
#include <string_view>
void processStringView(std::string_view sv) {
std::cout << "Viewing string slice: "" << sv << "" (Length: " << sv.length() << ")
";
}
int main() {
std::string text = "OperateTools High Performance Engine";
text.append(" - v2.0");
std::cout << "Full String: " << text << "
";
size_t pos = text.find("Performance");
if (pos != std::string::npos) {
std::cout << "'Performance' found at index: " << pos << "
";
}
// Zero-copy substring via string_view (C++17)
std::string_view viewSlice(text.data() + 13, 11);
processStringView(viewSlice);
return 0;
}Interview Question & Tips
A: SSO is an optimization technique where `std::string` uses its internal stack storage buffer to hold short strings (usually up to 15-22 bytes) without triggering expensive heap memory allocations via `malloc`/`new`.
Q: When should you use std::string_view instead of const std::string&?
A: Use `std::string_view` for read-only string parameters because it accepts `const char*` string literals, `std::string`, and substrings without allocating temporary `std::string` copies.
Best Practices
2. Use `text.reserve(N)` before appending in loops to prevent multiple memory reallocations.
3. Check `find()` return values against `std::string::npos` before using positions.
This study reference for Strings in C++: Character Sequences, std::string, and Common Operations is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: