Foundations
These are must-know before solving problems.
-
Mutable vs Immutable (C++: std::string is mutable)
-
Character arrays vs string class
-
Common operations: length, substring, concatenation, comparison
-
ASCII & Unicode basics
π 1. Mutable vs Immutable Strings
In C++:
β
std::string is mutable
You can change characters in place.
string s = "hello";
s[0] = 'H'; // modifies original string
After modification β "Hello"
β Character arrays are also mutable (as long as not stored in read-only memory)
char arr[] = "hello";
arr[1] = 'a'; // works
BUT this is NOT allowed:
char* s = "hello";
s[1] = 'a'; // β undefined behavior (string literal is read-only)
π Takeaway
| Type | Mutable? | Notes |
|---|---|---|
std::string |
β Yes | Safe, dynamic size |
char arr[] |
β Yes | Static size |
char* s = "literal" |
β No | Read-only memory |
π 2. Character Arrays vs std::string
π· Character Array (char[])
-
Fixed size
-
Ends with \0 (null terminator)
-
Needs manual handling
char name[10] = "Ajay";
If you modify without checking size β risk of overflow.
πΆ std::string (Recommended)
-
Dynamically grows
-
Supports many operations
-
Easier to use
string name = "Ajay";
name += " Gupta";
Differences in memory view:
Char array:
'A' 'j' 'a' 'y' '\0'
std::string internals:
size, capacity, pointer β ['A','j','a','y']
π Conclusion:
Use std::string unless you specifically need low-level C-style operations.
π 3. Common String Operations
Letβs see the most used methods in DSA.
πΉ Length
string s = "hello";
int len = s.length(); // or s.size()
πΉ Substring
string s = "abcdef";
string sub = s.substr(1, 3);
// starts at index 1, length 3 β "bcd"
πΉ Concatenation
string a = "Ajay";
string b = "Gupta";
string c = a + " " + b;
πΉ Comparison
Lexicographical comparison (dictionary order):
string a = "apple";
string b = "banana";
if (a < b) cout << "apple comes first";
Character comparison uses ASCII values.
πΉ Search operations
string s = "hello world";
int pos = s.find("world"); // returns 6
If not found β returns string::npos.
πΉ Access characters
char ch = s[2];
s[3] = 'x';
π std::to_string() β Overview
Converts numeric values to std::string
π Example: Convert int to string
int x = 42;
string s = to_string(x);
cout << s; // "42"
π Example: Using inside concatenation
int age = 25;
string info = "Age = " + to_string(age);
cout << info;
β οΈ Precision Issue (Important!)
to_string(double) always prints 6 decimal places.
to_string(3.1) β "3.100000"
If you want precise formatting, use:
#include <sstream>
stringstream ss;
ss << fixed << setprecision(2) << 3.14159;
string s = ss.str(); // "3.14"
β stringstream
-
A general formatting tool
-
Converts any type to string
-
Allows custom formatting
-
Useful for concatenation, parsing, precision control
Example:
stringstream ss;
ss << "Value = " << 42;
string s = ss.str();
β Use stringstream when:
-
You need precision control
-
You need formatting like:
-
padding
-
width
-
hex / oct / binary
-
-
Youβre converting multiple types in one stream
-
Example:
ss << "x=" << x << ", y=" << y;
π 4. ASCII & Unicode Basics
πΉ ASCII
-
0β127values -
C++ char is typically 1 byte
-
Useful for lowercase/uppercase checks
char c = 'a';
int val = (int)c; // 97 (ASCII of 'a')
Character manipulation using ASCII:
char up = c - 32; // 'a' β 'A'
πΉ Unicode
Supports all world languages.
C++ uses:
-
char16_t(UTF-16) -
char32_t(UTF-32) -
wstring(wide strings)
For DSA interviews: π ASCII-based problems are 95% of what you need π Unicode only matters in advanced parsing tasks