๐
Date: Jan 26, 2026
๐ฅ Topic: Standard Template Library (STL) String Functions
๐ ️ Why use the String Class?
In C, we had to manage character arrays manually (very painful). In C++, we have the <string> class which comes with superpowers (Built-in functions).
๐งฐ The Toolkit
Here are the most useful functions I learned today:
| Function | Description |
|---|---|
str.length() |
Returns the count of characters. |
str.push_back('x') |
Adds a character to the end. |
str.pop_back() |
Removes the last character. |
str.append("..") |
Joins another string at the end. |
str.find("abc") |
Searches for "abc" and returns index. |
๐ป Day 26 Code: String Playground
#include <iostream>
#include <string> // Important Header
using namespace std;
int main() {
string str = "Sahil";
// 1. Length
cout << "Length: " << str.length() << endl;
// 2. Modify
str.push_back('!');
cout << "After Push: " << str << endl;
// 3. Append
str.append(" Codes");
cout << "After Append: " << str << endl;
// 4. Substring (Extracting part of string)
// substr(startIndex, length)
string sub = str.substr(0, 5);
cout << "Extracted Name: " << sub << endl;
return 0;
}
๐ญ Thoughts
These functions save so much time. Imagine writing a loop just to find the length of a word every time. std::string handles memory management automatically, so no segmentation faults!
No comments:
Post a Comment