Zone Of Makos

Menu icon

String Manipulation in C++

String manipulation is a common task in programming, and C++ provides various built-in functions and libraries to handle strings efficiently. In this lesson, we'll explore different ways to manipulate strings in C++ and perform operations such as concatenation, substring extraction, searching, and modifying strings.

Working with C-Style Strings

In C++, strings can be represented as C-style strings, which are arrays of characters terminated by a null character ('\0'). To manipulate C-style strings, you can use functions from the cstring header, such as strlen , strcpy , strcat , and strcmp . These functions allow you to find the length of a string, copy one string to another, concatenate strings, and compare strings, respectively.


#include <iostream>
#include <cstring>

int main() {
    char str1[20] = "Hello";
    char str2[20] = " World";

    // Length of a string
    int length = strlen(str1);
    std::cout << "Length of str1: " << length << std::endl;

    // Concatenation of strings
    strcat(str1, str2);
    std::cout << "Concatenated string: " << str1 << std::endl;

    // Comparison of strings
    int result = strcmp(str1, str2);
    if (result == 0) {
        std::cout << "Strings are equal" << std::endl;
    } else {
        std::cout << "Strings are not equal" << std::endl;
    }

    return 0;
}

Working with C++ Strings

In addition to C-style strings, C++ also provides a string class in the string header, which offers a more convenient and flexible way to manipulate strings. The string class supports various member functions for string manipulation, such as length , substr , find , and replace . These functions allow you to find the length of a string, extract substrings, search for substrings, and replace substrings, respectively.


#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello";
    std::string str2 = " World";

    // Length of a string
    int length = str1.length();
    std::cout << "Length of str1: " << length << std::endl;

    // Concatenation of strings
    std::string concatenatedString = str1 + str2;
    std::cout << "Concatenated string: " << concatenatedString << std::endl;

    // Finding a substring
    size_t pos = concatenatedString.find("World");
    if (pos != std::string::npos) {
        std::cout << "Substring found at position: " << pos << std::endl;
    } else {
        std::cout << "Substring not found" << std::endl;
    }

    // Replacing a substring
    std::string replacedString = concatenatedString;
    size_t replacePos = replacedString.find("World");
    if (replacePos != std::string::npos) {
        replacedString.replace(replacePos, 5, "Universe");
        std::cout << "Replaced string: " << replacedString << std::endl;
    }

    return 0;
}

Conclusion

String manipulation is an essential aspect of programming, and in C++, you have multiple options for working with strings. You can use C-style strings and the functions from the cstring header or leverage the string class in the string header for more convenient string manipulation. Understanding these techniques will enable you to perform various operations on strings efficiently and effectively in your C++ programs.