Strings in Cpp

Strings in C++ are sequences of characters that are used to represent and manipulate textual data. They are commonly used for storing names, sentences, and other textual information in computer programs.

C-style strings in C++ are arrays of characters terminated by a null character (''). They require manual memory management and have limited functionality. C++ strings, on the other hand, are objects of the std::string class from the C++ Standard Library. They provide a higher-level interface, automatic memory management, and a wide range of built-in functions for string manipulation.

C++ strings offer several benefits over C-style strings. They simplify string handling by providing dynamic memory management, automatic resizing, and a rich set of functions for string manipulation. C++ strings support concatenation, comparison, search, and other operations, making them more convenient and efficient to work with. Additionally, C++ strings are compatible with other C++ Standard Library containers and algorithms, enhancing code readability and maintainability.

Declaring and Initializing Strings

A. Declaration and Initialization Syntax

In C++, you can declare and initialize a string using the std::string class from the C++ Standard Library. The syntax for declaring a string variable is as follows:

std::string str;

This declares a string variable named str without initializing it.

To initialize a string at the time of declaration, you can use one of the following syntaxes:

std::string str1 = "Hello";
std::string str2("World");
std::string str3 {"Welcome"};

These syntaxes initialize the string variables str1, str2, and str3 with the specified values.

B. Assigning Values to Strings

You can assign a new value to a string variable using the assignment operator = or the assign() member function. Examples:

std::string str = "Hello";
str = "Hi"; // Using assignment operator
str.assign("Hey"); // Using assign() member function

C. Concatenating Strings

To concatenate strings in C++, you can use the + operator or the append() member function. Examples:

std::string stri = "Hello";
std::string str2 = " World";
std::string resulti= strl + str2; // Using + operator
std::string result2 = strl.append(str2); // Using append() member function

Both result1 and result2 will contain the concatenated string "Hello World".

Note : The C++ Standard Library provides various functions and methods for string concatenation and manipulation, such as the += operator, push_back(), insert(), etc.

Accessing and Manipulating Strings

A. Accessing Individual Characters

In C++, you can access individual characters in a string using the indexing operator [] or the at() member function. Examples:

std::string str = "Hello";
char firstChar = str[0]; // Accessing the first character using indexing
char secondChar = str.at(1); // Accessing the second character using at()

B. Modifying Characters in a String

To modify characters in a string, you can use the indexing operator [] or the at() member function to access the desired character and assign a new value to it. Examples:

std::string str = "Hello";
str[0]='h'; // Modifying the first character to 'h'
str.at(4)='p'; // Modifying the fifth character to 'p'

C. Finding the Length of a String

You can find the length (number of characters) of a string using the length() or size() member functions. Examples:

std::string str = "Hello";
int length1 = str.length(); // Using length() member function
int length2 = str.size(); // Using size() member function

Both length1 and length2 will be equal to 5 in this case.

D. Searching for Substrings

To search for a substring within a string, you can use the find() member function, which returns the position of the first occurrence of the substring. If the substring is not found, it returns std::string::npos. Examples:

std::string str = "Hello World";
size_t found = str.find("World"); // Searching for "World" substring
if (found != std::string::npos) {
    // Substring found
} else {
    // Substring not found
}

E. Replacing Substrings

To replace a substring with another substring in a string, you can use the replace() member function. Examples:

std::string str = "Hello World";
size_t found = str.find("World"); // Searching for "World" substring
if (found != std::string::npos) {
    // Substring found
} else {
    // Substring not found
}

After this operation, the string str will be "Hello Universe".

Note : The C++ Standard Library provides various other functions and methods for string manipulation, such as substr(), compare(), erase(), etc., which allow for more advanced operations on strings.


Learn via Video Course

C++ Programming (English) Logo

C++ Programming (English)

2056

18 hrs