In C++, a string is a sequence of characters stored in a contiguous memory block. Here are some common string operations in C++ with code examples:
Declaration and Initialization : #include <iostream> #include <string> using namespace std; // Declaring and initializing a string variable string str = "Hello, World!"; String Concatenation: // Using the + operator to concatenate two strings string str1 = "Hello"; string str2 = "World"; string result = str1 + ", " + str2 + "!"; cout << result << endl; // Output: "Hello, World!" String Length: // Using the length() function to get the length of a string string str = "Hello, World!"; int len = str.length(); cout << len << endl; // Output: 13 String Search: // Using the find() function to search for a substring within a string string str = "Hello, World!"; int index = str.find("World"); if (index != string::npos) { cout << "Found at index " << index << endl; } else { cout << "Not found" << endl; } // Output: "Found at index 7"
PRACTICE PROBLEMS :