Skip to content
ajdevhub
3 min read

📑 Table of Contents

  1. Foundations 1.1. 📘 1. Mutable vs Immutable Strings 1.2. 📘 2. Character Arrays vs std::string 1.3. 📘 3. Common String Operations 1.4. 📘 4. ASCII & Unicode Basics

🎯 Main Topics Covered

  1. 1 Mutable vs Immutable Strings
  2. 2 Character Arrays vs stdstring
  3. 3 Common String Operations
  4. 4 ASCII Unicode Basics

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

TypeMutable?Notes
std::string✅ YesSafe, dynamic size
char arr[]✓ YesStatic size
char* s = "literal"❌ NoRead-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.

  • 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–127 values

  • 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