Skip to content

C++ Strings

Overview

C++ provides two main string types: C-style strings (character arrays) and the modern std::string class from the STL.

C-style Strings

Basic C-style Strings

cpp
#include <iostream>
#include <cstring>

int main() {
    // C-style string declaration
    char str1[] = "Hello, World!";
    char str2[20];
    
    // String copying
    strcpy(str2, str1);
    std::cout << "Copied string: " << str2 << std::endl;
    
    // String length
    size_t len = strlen(str1);
    std::cout << "Length: " << len << std::endl;
    
    // String concatenation
    char str3[50];
    strcpy(str3, str1);
    strcat(str3, " How are you?");
    std::cout << "Concatenated: " << str3 << std::endl;
    
    return 0;
}

std::string (Modern C++)

Basic std::string Operations

cpp
#include <iostream>
#include <string>

int main() {
    // String declaration and initialization
    std::string s1 = "Hello";
    std::string s2("World");
    std::string s3(10, 'A');  // "AAAAAAAAAA"
    
    std::cout << "s1: " << s1 << std::endl;
    std::cout << "s2: " << s2 << std::endl;
    std::cout << "s3: " << s3 << std::endl;
    
    // String concatenation
    std::string s4 = s1 + ", " + s2 + "!";
    std::cout << "s4: " << s4 << std::endl;
    
    // String length
    std::cout << "Length of s4: " << s4.length() << std::endl;
    std::cout << "Size of s4: " << s4.size() << std::endl;
    
    return 0;
}

String Access and Modification

cpp
#include <iostream>
#include <string>

int main() {
    std::string text = "Hello, World!";
    
    // Access characters
    std::cout << "First character: " << text[0] << std::endl;
    std::cout << "Last character: " << text[text.length() - 1] << std::endl;
    
    // Modify characters
    text[0] = 'J';
    std::cout << "Modified: " << text << std::endl;
    
    // Substring
    std::string sub = text.substr(7, 5);  // "World"
    std::cout << "Substring: " << sub << std::endl;
    
    // Find and replace
    size_t pos = text.find("World");
    if (pos != std::string::npos) {
        text.replace(pos, 5, "C++");
    }
    std::cout << "After replace: " << text << std::endl;
    
    return 0;
}

String Input/Output

cpp
#include <iostream>
#include <string>

int main() {
    std::string name;
    
    // Input with spaces
    std::cout << "Enter your full name: ";
    std::getline(std::cin, name);
    std::cout << "Hello, " << name << "!" << std::endl;
    
    // Input single word
    std::string word;
    std::cout << "Enter a word: ";
    std::cin >> word;
    std::cout << "You entered: " << word << std::endl;
    
    return 0;
}

String Comparison

cpp
#include <iostream>
#include <string>

int main() {
    std::string s1 = "Hello";
    std::string s2 = "hello";
    std::string s3 = "Hello";
    
    // Comparison operators
    if (s1 == s3) {
        std::cout << "s1 equals s3" << std::endl;
    }
    
    if (s1 != s2) {
        std::cout << "s1 does not equal s2" << std::endl;
    }
    
    // Case-sensitive comparison
    if (s1 < s2) {
        std::cout << "s1 comes before s2 alphabetically" << std::endl;
    }
    
    // Case-insensitive comparison
    std::string s1_lower = s1;
    std::string s2_lower = s2;
    
    // Convert to lowercase for comparison
    for (char& c : s1_lower) c = tolower(c);
    for (char& c : s2_lower) c = tolower(c);
    
    if (s1_lower == s2_lower) {
        std::cout << "s1 and s2 are the same (case-insensitive)" << std::endl;
    }
    
    return 0;
}

String Stream Operations

cpp
#include <iostream>
#include <string>
#include <sstream>

int main() {
    // String to number conversion
    std::string num_str = "123.45";
    std::stringstream ss(num_str);
    double number;
    ss >> number;
    std::cout << "Number: " << number << std::endl;
    
    // Number to string conversion
    int value = 42;
    std::stringstream ss2;
    ss2 << value;
    std::string result = ss2.str();
    std::cout << "String: " << result << std::endl;
    
    // Splitting string
    std::string data = "apple,banana,cherry,date";
    std::stringstream ss3(data);
    std::string item;
    
    while (std::getline(ss3, item, ',')) {
        std::cout << "Item: " << item << std::endl;
    }
    
    return 0;
}

Content is for learning and research only.