⏱ 4 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

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.

  • 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