# cstring

***

**1. Memory Management:**

```c++
char* str = new char[strlen("Hello World") + 1];
strcpy(str, "Hello World");
// ... use str ...
delete[] str;
```

**2. String Initialization:**

```c++
char str[] = "Hello World";  // null-terminated character array
string str = "Hello World";  // C++ string
```

**3. String Length:**

```c++
size_t len = strlen("Hello World");
```

**4. String Concatenation:**

```c++
char* result = strcat(str1, str2);
```

**5. String Comparison:**

```c++
int comparison = strcmp(str1, str2);
```

**6. Character Access:**

```c++
char c = str[0];
```

**7. String Modification:**

```c++
str[0] = 'H';
```

**8. String Copy:**

```c++
strcpy(dest, src);
```

**9. String Search:**

```c++
char* found = strstr(str, "World");
```

**10. String Tokenization:**

```c++
char* token = strtok(str, " ");
```

**11. String Formatting:**

```c++
sprintf(buffer, "%.2f", value);
```

**12. String to Number Conversion:**

```c++
int num = atoi("123");
```

**13. Number to String Conversion:**

```c++
string str = to_string(123);
```

**14. String to Floating-Point Conversion:**

```c++
float f = atof("123.45");
```

**15. Floating-Point to String Conversion:**

```c++
string str = to_string(123.45);
```

**16. String to Character Array:**

```c++
char* arr = str.c_str();
```

**17. Character Array to String:**

```c++
string str(arr);
```

**18. String Substring:**

```c++
string sub = str.substr(5, 4);
```

**19. String Find:**

```c++
size_t pos = str.find("World");
```

**20. String Replace:**

```c++
str.replace(5, 4, "Universe");
```

**21. String Trim:**

```c++
str.trim();
```

**22. String To Uppercase:**

```c++
str.toUpper();
```

**23. String To Lowercase:**

```c++
str.toLower();
```

**24. String Split:**

```c++
vector<string> words = str.split(" ");
```

**25. String Join:**

```c++
string joined = str.join(words, ", ");
```

**26. String Reverse:**

```c++
reverse(str.begin(), str.end());
```

**27. String Sort:**

```c++
sort(str.begin(), str.end());
```

**28. String Encryption:**

```c++
string encrypted = encrypt(str, key);
```

**29. String Decryption:**

```c++
string decrypted = decrypt(encrypted, key);
```

**30. String Hashing:**

```c++
string hash = hash(str);
```

**31. String Compression:**

```c++
string compressed = compress(str);
```

**32. String Decompression:**

```c++
string decompressed = decompress(compressed);
```

**33. String Conversion to Hexadecimal:**

```c++
string hex = str.toHex();
```

**34. String Conversion from Hexadecimal:**

```c++
string str = hex.fromHex();
```

**35. String Padding:**

```c++
string padded = str.pad(10, '0');
```

**36. String Truncation:**

```c++
string truncated = str.truncate(10);
```

**37. String Encoding:**

```c++
string encoded = str.encode(encoding);
```

**38. String Decoding:**

```c++
string decoded = str.decode(encoding);
```

**39. String Validation:**

```c++
bool valid = str.isValid();
```

**40. String Formatting for Numbers:**

```c++
string formatted = str.format(value, "0.00");
```

**41. String Formatting for Dates:**

```c++
string formatted = str.format(date, "yyyy-MM-dd");
```

**42. String Formatting for Currency:**

```c++
string formatted = str.format(currency, "$0.00");
```

**43. String Formatting for Percentages:**

```c++
string formatted = str.format(percent, "0.00%");
```

**44. String Formatting for Hours and Minutes:**

```c++
string formatted = str.format(time, "hh:mm a");
```

**45. String Argument Parsing:**

```c++
int num = stoi(argv[1]);
string option = argv[2];
```

**46. String Output Manipulation:**

```c++
cout << left << setw(10) << str;
```

**47. String Input Manipulation:**

```c++
cin >> setw(10) >> str;
```

**48. String Manipulation with Regular Expressions:**

```c++
regex r(".*Hello.*");
regex_match(str, r);
```

**49. String Manipulation with Boost:**

```c++
boost::algorithm::trim(str);
boost::algorithm::replace_all(str, " ", "_");
```

**50. String Manipulation with C++20:**

```c++
string str(u8"你好世界");
auto result = string_view{str}.starts_with("你");
```
