8. DSA with C++ - Strings and its operations

Strings and its operation

In C++, strings are represented using the std::string class from the <string> header. The std::string class provides a wide range of properties and operations for working with strings. Here's an explanation of strings along with their syntax and examples: String Declaration and Initialization: You can declare and initialize a string using the following syntax:

std::string strName = "Hello, World!";
    
For example:

std::string greeting = "Hello, there!";
    
String Concatenation: You can concatenate two strings using the + operator or the append() function.

std::string concatenatedStr = str1 + str2;
    
For example:

std::string name = "John";
std::string greeting = "Hello, " + name;
    
String Length: To get the length of a string, you can use the length() or size() member functions.

int strLength = strName.length();
    
For example:

std::string message = "Hello, World!";
int length = message.length();
    
String Access: You can access individual characters of a string using the array index notation [].

char ch = strName[index];
    
For example:

std::string word = "Hello";
char firstLeƩer = word[0];
    
String Comparison: To compare two strings, you can use the comparison operators ==, !=, <, >, <=, >=, or use the compare() member function.

bool isEqual = (str1 == str2);
    
For example:

std::string name1 = "John";
std::string name2 = "Jane";
bool isSameName = (name1 == name2);
    
String Substring: You can extract a substring from a string using the substr() member function.

std::string substring = strName.substr(startIndex, length);
    
For example:

std::string fullName = "John Doe";
std::string lastName = fullName.substr(5, 3);
    
String Searching: To search for a substring within a string, you can use the find() member function.

size_t position = strName.find(substring);
    
For example:

std::string sentence = "I love programming!";
size_t position = sentence.find("programming");
    
String Modification: You can modify a string by assigning a new value to it or using various member functions such as replace(), insert(), erase(), etc.

strName = "New Value"; // Assign a new value
strName.replace(startIndex, length, replacement); // Replace substring
strName.insert(position, substring); // Insert substring
strName.erase(startIndex, length); // Erase substring
    
For example:

std::string message = "Hello, World!";
message.replace(7, 5, "OpenAI");
message.insert(0, "Greetings: ");
message.erase(12, 6);
    
These are some of the commonly used properties and operations for working with strings in C++. The std::string class provides a rich set of functions that make string manipulation and handling more convenient and efficient.

Certainly! Here are some more operations commonly performed on strings in C++:

1. String Conversion:

You can convert a string to other data types like integers or floating-point numbers using functions such as stoi() (string to integer), stol() (string to long integer), stof() (string to float), and stod() (string to double).

std::string numberStr = "123";
int number = std::stoi(numberStr);
    
2. String to C-Style String Conversion: To convert a C++ string to a C-style string (null-terminated character array), you can use the c_str() member function.

std::string str = "Hello";
const char* cstr = str.c_str();
    
3. String Case Conversion: You can convert a string to lowercase or uppercase using the tolower() and toupper() functions from the <cctype> header or the transform() function from the <algorithm> header.

std::string str = "Hello, World!";
std::transform(str.begin(), str.end(), str.begin(), ::tolower); // Convert to lowercase
    
4. String Spliƫng: To split a string into substrings based on a delimiter, you can use the getline() or find() functions along with string streams or the substr() function.

std::string str = "Apple,Banana,Orange";
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, ',')) {
 // Process each token
}
    
5. String Joining: To join multiple strings together, you can use the + operator or the append() function.

std::string str1 = "Hello";
std::string str2 = "World!";
std::string joinedStr = str1 + " " + str2; // Join using +
    
6. String Trimming: To remove leading and trailing whitespace characters from a string, you can use the find_first_not_of() and find_last_not_of() functions along with the substr() function.

std::string str = " Hello, World! ";
str = str.substr(str.find_first_not_of(" \t\r\n"), str.find_last_not_of(" \t\r\n") + 1);
    
7. String Reversal: To reverse the characters in a string, you can use the reverse() function from the <algorithm> header.

std::string str = "Hello, World!";
std::reverse(str.begin(), str.end());
    
8. String Padding: To pad a string with a specific character to a certain length, you can use the std::setw() and std::seƞill() manipulators from the <iomanip> header.

#include <iostream>
#include <iomanip>
int main() {
 std::string str = "Hello";
 std::cout << std::setw(10) << std::seƞill('*') << str << std::endl; // Output: *****Hello
 return 0;
}