String In C++
When a software stores, manipulates, and processes text sequences, it is said to be using string handling in C++. The more contemporary, object-oriented Standard C++ std::string class and the more conventional C-style strings (null-terminated character arrays) are the two main methods for managing strings in C++.
C-Style Character Strings
With a special null character (\0) at the end, C-style strings are basically arrays of characters. This null character marks the string’s end. Their efficiency, control, and compatibility with earlier C code make them an essential component of C and the reason they are still frequently used in C++.
Declaration and Initialization
- It is possible to initialise them with string literals. char helloworld[] = “Hello, world!”; puts the null character in automatically, for instance.
- It is possible to assign individual characters, however the null terminator must be manually added by the coder.
- An array of characters can buffer data transfer.
Operations
There are no direct manipulation operators for C-style strings. Instead, they use library methods from the (or string.h) header. The following are a few typical functions:
strcpy(destination, source): Sends a copy of the source string. If the destination array is too tiny, buffer overruns could occur because this function doesn’t do boundary checks.
strcat(destination, source): Joins the source and destination in the end.
strlen(string): Does not include the null terminator when returning the string’s length.
strcmp(string1, string2): A lexicographic comparison between two strings returns 0 if equal, -1 if less, and +1 if larger.
strstr(haystack, needle): Determines where in a haystack string a needle substring appears first.
Input/Output
- Formatted console I/O is handled by printf() and scanf() procedures in (or stdio.h).
- Gets reads a string from standard input. But it’s dangerous because it doesn’t check boundaries and can cause buffer overruns. It is safer to use fgets() instead.
- A string is written to standard output with a newline added by using puts(char *str).
Limitations
Static (fixed-size arrays), less secure because of manual memory management, and lacking direct support for C++ operators for popular string operations like assignment and concatenation are all characteristics of C-style strings.
Code Example
#include <iostream> // For standard input/output (cin, cout)
#include <cstring> // For C-style string functions (strlen, strcpy, strcat, strcmp)
int main() {
std::cout << "--- C-Style String Examples ---" << std::endl;
// Declaration and Initialization
char c_str1[181] = "Hello"; // Initialized with a string literal
char c_str2[181]; // Uninitialized character array
// Copying a string (strcpy)
std::strcpy(c_str2, c_str1);
std::cout << "c_str1: " << c_str1 << std::endl;
std::cout << "c_str2 (copied from c_str1): " << c_str2 << std::endl;
// Concatenating strings (strcat)
char c_str3[183] = "World";
std::strcat(c_str1, " C++"); // c_str1 now "Hello C++"
std::strcat(c_str3, " Programming"); // c_str3 now "World Programming"
std::cout << "c_str1 after strcat: " << c_str1 << std::endl;
std::cout << "c_str3 after strcat: " << c_str3 << std::endl;
// Getting string length (strlen)
int len_c_str1 = std::strlen(c_str1);
std::cout << "Length of c_str1: " << len_c_str1 << std::endl;
// Comparing strings (strcmp)
// Returns 0 if equal, <0 if first is lexicographically smaller, >0 otherwise
if (std::strcmp(c_str1, "Hello C++") == 0) {
std::cout << "c_str1 is equal to \"Hello C++\"" << std::endl;
}
std::cout << "\n"; // Newline for better readability
return 0;
}
Output
--- C-Style String Examples ---
c_str1: Hello
c_str2 (copied from c_str1): Hello
c_str1 after strcat: Hello C++
c_str3 after strcat: World Programming
Length of c_str1: 9
c_str1 is equal to "Hello C++"
The Standard C++ Class
Std::string, a crucial component of the C++ Standard Library, handles character sequences more reliably, easily, and securely than C-style strings. It is defined in the header of the std namespace. Since char is the character type of basic_string, std::string is actually a specialisation of that more generic template class.
Key Advantages
Automatic Memory Control: Std::string objects allocate and deallocate memory without programmer intervention.
Operator Overloading: C++ operations like assignment (=), concatenation (+, +=), and comparison (==,!=, <, <=, >, >=) can be easily implemented using std::string. Thus, they behave as built-in types.
Safety: Buffer overruns, which are common with C-style strings, are avoided by it.
Initialization and Assignment
There are other ways to initialize std::string objects, including:
- The initialization is set to a blank string.
- taking a copy of a string literal or another std::string.
- using a predetermined quantity of the same characters.
Core Member Functions and Operations
I/O routines std::cout < myString and std::cin >> cater to input and output. I use the overloaded << and >> operators in myString. >> reads whitespace-separated words. You can use the global getline(std::cin, myString) function to read complete lines.
Size and Capacity:
- size() and length(): Both provide back the string’s character count.
- empty(): If the string is character-free, it returns true.
- capacity(): Returns the string’s entire memory allocation, which may be greater than its size() at this point to accommodate expansion.
- max_size(): Gives the largest size that a string object can have.
Character Access:
Using the [] operator or the at() method, one can access individual characters. While at() offers range checking and raises an out_of_range exception in the event that the index is invalid, [] is more efficient.
Modification:
- assign(): Changes all of the string’s contents.
- append(): Adds a string or additional characters at the end.
- insert(): Adds characters to a designated location.
- erase(): Extracts characters from a designated location.
- replace(): Changes a section of the string to a different one.
- substr(pos, len): Gives back a new string that is a clone of the original.
- swap(otherString): Sends and receives two string objects.
Conversion to C-style String
To make C-style string functions compatible, the member functions c_str() and data() return a null-terminated const char* pointer.
String Streams (<sstream>)
Istringstream, ostringstream, and stringstream enable in-memory input/output by treating strings as input/output streams. This helps process and format string data. For example, std::ostringstream can convert numbers to std::strings.
Standard Template Library (STL)
std::string is a component of the C++ Standard Library, but it completely interacts with the generic programming model of the Standard Template Library and is frequently seen as a container in and of itself. Accordingly, std::string supports:
Iterators: Iterators are generalizations of pointers that are used to traverse the characters in a string, and it has begin() and end() methods to retrieve them.
Algorithms: Iterators are used to perform operations on std::string objects by many STL algorithms (such as std::sort, std::count, and std::transform) from the header.
Code Example: String Handling in C++
The std::string class example to illustrate common string handling operations.
#include <iostream> // For standard input/output (cin, cout)
#include <string> // For std::string class
#include <algorithm> // For std::sort and other algorithms with std::string
int main() {
// Declaration and Initialization
std::string s1 = "Hello"; // Initialized from a string literal
std::string s2("World"); // Another way to initialize
std::string s3; // Default-initialized (empty string)
std::string s4(5, 'X'); // Initializes with 5 'X' characters
std::cout << "s1: " << s1 << std::endl;
std::cout << "s2: " << s2 << std::endl;
std::cout << "s3 (empty): " << s3 << std::endl;
std::cout << "s4 (5 'X's): " << s4 << std::endl;
// Assignment operation (=)
s3 = s1; // s3 is now a copy of s1
std::cout << "s3 after assignment from s1: " << s3 << std::endl;
// Concatenation using + and += operators
std::string combined_str = s1 + " " + s2;
std::cout << "s1 + s2: " << combined_str << std::endl;
combined_str += "!"; // Appends to the string
std::cout << "combined_str after appending '!': " << combined_str << std::endl;
// Getting string length/size (length() and size())
std::cout << "Length of combined_str: " << combined_str.length() << std::endl;
std::cout << "Size of s4: " << s4.size() << std::endl;
// Accessing characters (at() and [] operator)
std::cout << "First character of s1: " << s1.at(0) << std::endl;
std::cout << "Second character of s2: " << s2[1] << std::endl;
// Modifying strings (insert, erase, replace)
s1.insert(5, " there"); // Insert " there" at index 5
std::cout << "s1 after insert: " << s1 << std::endl; // "Hello there"
s1.erase(5, 6); // Erase 6 characters starting at index 5 (" there")
std::cout << "s1 after erase: " << s1 << std::endl; // "Hello"
combined_str.replace(6, 5, "Big World"); // Replace "World" with "Big World"
std::cout << "combined_str after replace: " << combined_str << std::endl; // "Hello Big World!"
// Substring (substr)
std::string sub = combined_str.substr(0, 5); // Get "Hello"
std::cout << "Substring from combined_str: " << sub << std::endl;
// Comparing strings (==, >, compare())
if (s1 == "Hello") { // Using == operator
std::cout << "s1 is equal to \"Hello\"" << std::endl;
}
if (s2.compare("Alpha") > 0) { // Using compare() method
std::cout << "s2 (\"World\") is lexicographically greater than \"Alpha\"" << std::endl;
}
// Inputting a string from console using getline()
std::string user_input;
std::cout << "\nEnter a line of text (including spaces): ";
std::getline(std::cin >> std::ws, user_input); // std::ws consumes leading whitespace
std::cout << "You entered: " << user_input << std::endl;
// Converting std::string to C-style string (c_str())
const char* c_style_from_std = user_input.c_str();
std::cout << "C-style version of your input: " << c_style_from_std << std::endl;
// Using std::string as a container with an algorithm (std::sort)
std::string unsorted_str = "programming";
std::cout << "Unsorted string: " << unsorted_str << std::endl;
std::sort(unsorted_str.begin(), unsorted_str.end());
std::cout << "Sorted string: " << unsorted_str << std::endl;
return 0;
}
Output
s1: Hello
s2: World
s3 (empty):
s4 (5 'X's): XXXXX
s3 after assignment from s1: Hello
s1 + s2: Hello World
combined_str after appending '!': Hello World!
Length of combined_str: 12
Size of s4: 5
First character of s1: H
Second character of s2: o
s1 after insert: Hello there
s1 after erase: Hello
combined_str after replace: Hello Big World!
Substring from combined_str: Hello
s1 is equal to "Hello"
s2 ("World") is lexicographically greater than "Alpha"
Enter a line of text (including spaces): he Hello Every one
You entered: Hello Every one
C-style version of your input: Hello Every one
Unsorted string: programming
Sorted string: aggimmnoprr