Strings in C++

A string in C++ is a sequence of characters. Strings are stored in contiguous memory locations, which means that the characters in a string are stored next to each other in memory.

String Declaration and Initialization

You can declare and initialize strings using double quotes.

std::string greeting = "Hello, World!"; // Declares and initializes a string

String Operations

The std::string class provides a number of methods for performing operations on strings. For example, the following methods can be used to:

  1. Concatenate two strings:+ operator or std::string::append() method
  2. Find the length of a string:std::string::length() method
  3. Find the index of a character in a string:std::string::find() method
  4. Extract a substring from a string:std::string::substr() method
Example
std::string firstName = "Demil"; std::string lastName = "John"; std::string fullName = firstName + " " + lastName; // String concatenation int length = fullName.length(); // Get the length of the string char firstChar = fullName[0]; // Access the first character

String Input and Output

You can use the input and output streams (cin and cout) to read and display strings.

std::string userInput; std::cout << "Enter your name: "; std::cin >> userInput; // Read a string from the user std::cout << "You entered: " << userInput << std::endl; // Display the string

String Comparison

You can compare strings using comparison operators (e.g., ==, !=) and other comparison functions.

std::string password = "secret123"; if (userInput == password) { std::cout << "Access granted!" << std::endl; } else { std::cout << "Access denied!" << std::endl; }

String Manipulation

C++ provides various functions for string manipulation, such as finding substrings, replacing text, and converting to uppercase or lowercase.

std::string text = "C++ is a powerful language."; size_t found = text.find("powerful"); // Find a substring text.replace(found, 8, "versatile"); // Replace a substring std::transform(text.begin(), text.end(), text.begin(), ::tolower); // Convert to lowercase

String Concatenation Shortcut

You can use the += operator to append one string to another.

std::string message = "Hello, "; message += "C++"; // Appends "C++" to the end of the string

Iterating over strings

You can use a for loop to iterate over the characters in a string. The following for loop prints all of the characters in the string my_string to the console:

for (int i = 0; i < my_string.length(); i++) { std::cout << my_string[i] << std::endl; }

Here are some additional examples of using strings in C++:

// Concatenate two strings std::string my_string1 = "Hello, "; std::string my_string2 = "world!"; std::string my_new_string = my_string1 + my_string2; // Find the length of a string int string_length = my_new_string.length(); // Find the index of a character in a string int index_of_world = my_new_string.find("world!"); // Extract a substring from a string std::string my_substring = my_new_string.substr(7); // Print the results to the console std::cout << "my_new_string: " << my_new_string << std::endl; std::cout << "string_length: " << string_length << std::endl; std::cout << "index_of_world: " << index_of_world << std::endl; std::cout << "my_substring: " << my_substring << std::endl; //Output: my_new_string: Hello, world! string_length: 13 index_of_world: 7 my_substring: world!

Conclusion

Strings are sequences of characters stored as objects of the std::string class. They can be declared, initialized, manipulated, and compared easily, making them a versatile data type for handling textual information. C++ provides a comprehensive set of string operations, making it convenient to work with strings for tasks like concatenation, substring search, and text manipulation.