Header Files
- Character Arrays: Typically use
<cstring>(or<string.h>). std::string: Use<string>.
Examples and Analogous Functions
1. Initialization
| Character Array | std::string |
|---|---|
char str[] = "Hello"; | std::string str = "Hello"; |
2. Length of the String
| Character Array | std::string |
|---|---|
Header: <cstring> | Header: <string> |
size_t len = strlen(str); | size_t len = str.length(); |
strlen function is used to get the length. | .length() or .size() member functions. |
3. Copying Strings
| Character Array | std::string |
|---|---|
Header: <cstring> | Header: <string> |
char dest[10]; | std::string dest; |
strcpy(dest, src); | dest = src; |
strcpy function is used to copy strings. | Assignment operator (=) is used. |
4. Concatenation
| Character Array | std::string |
|---|---|
Header: <cstring> | Header: <string> |
char dest[20] = "Hello "; | std::string dest = "Hello "; |
strcat(dest, src); | dest += src; |
strcat function is used for concatenation. | += operator is used for concatenation. |
5. Comparison
| Character Array | std::string |
|---|---|
Header: <cstring> | Header: <string> |
if (strcmp(str1, str2) == 0) { /* ... */ } | if (str1 == str2) { /* ... */ } |
strcmp function is used for comparison. | Comparison operators (==, !=, <, >, etc.) are used. |
6. Input from User
| Character Array | std::string |
|---|---|
Header: <iostream> | Header: <iostream>, <string> |
| ```cpp | ```cpp |
| char str[50]; | std::string str; |
| std::cin.getline(str, 50); | std::getline(std::cin, str); |
| ``` | ``` |
std::cin.getline is used for input. | std::getline is used for input. |
Character Array Example
#include <iostream>#include <cstring>using namespace std;int main() {char str1[50] = "Hello";char str2[50];// Copying stringstrcpy(str2, str1);// Concatenationstrcat(str2, " World");// Lengthsize_t length = strlen(str2);// Comparisonif (strcmp(str1, "Hello") == 0) {cout << "str1 is equal to 'Hello'" << endl;}// Outputcout << "str2: " << str2 << endl;cout << "Length of str2: " << length << endl;return 0;}
std::string Example
#include <iostream>#include <string>using namespace std;int main() {string str1 = "Hello";string str2;// Copying stringstr2 = str1;// Concatenationstr2 += " World";// Lengthsize_t length = str2.length();// Comparisonif (str1 == "Hello") {cout << "str1 is equal to 'Hello'" << endl;}// Outputcout << "str2: " << str2 << endl;cout << "Length of str2: " << length << endl;return 0;}
#include <iostream>
#include <string>
using namespace std;
int main() {
// Initialize strings
string str = "Hello, World!";
string str2 = "C++ Programming";
string str3;
// 1. size() and length()
cout << "Size of str: " << str.size() << endl;
cout << "Length of str: " << str.length() << endl;
// 2. empty()
cout << "Is str empty? " << (str.empty() ? "Yes" : "No") << endl;
// 3. clear()
str3 = str; // Copying str to str3
str3.clear();
cout << "str3 after clear(): '" << str3 << "'" << endl;
// 4. operator= and operator+=
str = "New String";
str += " with more text";
cout << "str after +=: " << str << endl;
// 5. insert()
str.insert(4, " inserted");
cout << "str after insert(): " << str << endl;
// 6. erase()
str.erase(4, 9);
cout << "str after erase(): " << str << endl;
// 7. replace()
str.replace(4, 6, "replaced");
cout << "str after replace(): " << str << endl;
// 8. substr()
string sub = str.substr(4, 9);
cout << "Substring of str: " << sub << endl;
// 9. find()
size_t pos = str.find("replaced");
if (pos != string::npos) {
cout << "'replaced' found at position: " << pos << endl;
} else {
cout << "'replaced' not found" << endl;
}
// 10. rfind()
pos = str.rfind("replaced");
if (pos != string::npos) {
cout << "'replaced' found at position (rfind): " << pos << endl;
} else {
cout << "'replaced' not found (rfind)" << endl;
}
// 11. compare()
int cmpResult = str.compare(str2);
if (cmpResult == 0) {
cout << "str and str2 are equal" << endl;
} else if (cmpResult < 0) {
cout << "str is less than str2" << endl;
} else {
cout << "str is greater than str2" << endl;
}
// 12. c_str()
cout << "C-style string: " << str.c_str() << endl;
return 0;
}
OUTPUT
Size of str: 13
Length of str: 13
Is str empty? No
str3 after clear(): ''
str after +=: New String with more text
str after insert(): New inserted String with more text
str after erase(): New String with more text
str after replace(): New String replaced more text
Substring of str: String rep
'replaced' found at position: 13
'replaced' found at position (rfind): 13
str is less than str2
C-style string: New String replaced more text
Size of str: 13
Length of str: 13
Is str empty? No
str3 after clear(): ''
str after +=: New String with more text
str after insert(): New inserted String with more text
str after erase(): New String with more text
str after replace(): New String replaced more text
Substring of str: String rep
'replaced' found at position: 13
'replaced' found at position (rfind): 13
str is less than str2
C-style string: New String replaced more text
Characteristics of size_t
Unsigned: size_t is always an unsigned type, meaning it can only represent non-negative values.
Common Use Cases:
Representing the size of an object, such as arrays, strings, and memory blocks.
Used by various standard library functions for specifying sizes and counts.

Comments
Post a Comment
Feel Free to say
Confession !!!!