Skip to content

String Fundamentals

Char Arrays:

  • A char array is a sequence of characters stored in contiguous memory locations.
  • Char arrays in C/C++ are used to store strings but are not the same as string datatype.
Declaration:
char arr[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // \0 is the null terminator
Example:
char name[] = "John"; // automatically adds '\0' at the end
  • Important Points:
    • A char array must end with the null character \0 to represent a string.
    • If the null terminator is missing, it could lead to undefined behavior when used with string functions.
Accessing and Modifying:
char str[] = "World";
str[0] = 'w'; // Modifies the first character
cout << str; // Output: world

2. Strings (String Class):

  • The string class is part of the C++ Standard Library and provides a more powerful way to handle strings.
  • It eliminates the need for manual memory management and provides many useful functions.
Declaration:
string str = "Hello";
Example:
string s1 = "Hello";
string s2 = "World";
// Concatenation
string s3 = s1 + " " + s2; // "Hello World"
// Accessing elements
cout << s1[0]; // Output: H

3. Difference between Char Arrays and Strings:

Char ArrayString Class
Requires manual memory management.Memory management handled internally.
Functions like strcpy, strlen needed.Built-in methods like .length(), .substr()
Fixed size once declared.Dynamic in size.
Example: char arr[5] = "abc";Example: string s = "abc";
4. Common Operations on Char Arrays:
  • strlen(): Returns the length of the string (excluding \0).
char arr[] = "hello";
cout << strlen(arr); // Output: 5
  • strcpy(): Copies one string to another.
char src[] = "copy";
char dest[10];
strcpy(dest, src);
  • strcmp(): Compares two strings lexicographically
char str1[] = "abc";
char str2[] = "def";
int result = strcmp(str1, str2); // result < 0 because "abc" < "def"
5. Common String Class Functions:
  • .length(): Returns the length of the string
string str = "Hello";
cout << str.length(); // Output: 5
  • .substr(start, length): Extracts a substring.
string str = "Hello";
string sub = str.substr(1, 3); // "ell"
  • .find(substring): Finds the first occurrence of a substring
string str = "Hello World";
cout << str.find("World"); // Output: 6
6. Converting Between Char Arrays and Strings:
  • From char array to string:
char arr[] = "hello";
string str = string(arr); // Conversion to string
  • From string to char array
string str = "hello";
char arr[10];
strcpy(arr, str.c_str()); // Converts string to char array
Example of both:
#include <iostream>
#include <cstring> // For char array functions
#include <string> // For string class
using namespace std;
int main() {
// Char Array Example
char arr[] = "Hello";
cout << "Char Array Length: " << strlen(arr) << endl;
// String Example
string str = "World";
cout << "String Length: " << str.length() << endl;
return 0;
}