Contents

Strings

C++ Strings Overview

C++ offers two primary ways to handle strings: traditional C-style character arrays and the std::string class from the standard library, which provides a more flexible, dynamic approach.

1. C-Style Strings: C-style strings are simple character arrays ending with a null character ('\0'), inherited from the C language. They are efficient but lack the advanced features of the std::string class.

Example:

				
					#include <iostream>
using namespace std;

int main() {
    char s[] = "Programming";
    cout << s << endl;
    return 0;
}

				
			

Output:

				
					Programming

				
			

2. std::string Class : The std::string class, part of the <string> library, introduces numerous advantages over C-style strings, such as dynamic sizing and various member functions for easy manipulation.

				
					#include <iostream>
using namespace std;

int main() {
    string str("Hello, C++");
    cout << str;
    return 0;
}

				
			

Output:

				
					Hello, C++

				
			
Defining Strings with Repeating Characters

To define strings with repeated characters:

Example:

				
					#include <iostream>
using namespace std;

int main() {
    string str(4, 'A');
    cout << str;
    return 0;
}

				
			

Output:

				
					AAAA

				
			
Methods for Taking String Input

1. Using cin
2. Using getline
3. Using stringstream

1. Using cin: The simplest method is to use cin with the extraction operator (>>), which reads input until a space is encountered.

Example:

				
					#include <iostream>
using namespace std;

int main() {
    string s;
    cout << "Enter a word: ";
    cin >> s;
    cout << "Entered word: " << s << endl;
    return 0;
}

				
			

Output:

				
					Enter a word: Hello
Entered word: Hello

				
			

2. Using getline: The getline() function reads an entire line of input, including spaces.

Example:

				
					#include <iostream>
using namespace std;

int main() {
    string s;
    cout << "Enter a sentence: ";
    getline(cin, s);
    cout << "Entered sentence: " << s << endl;
    return 0;
}

				
			

Output:

				
					Enter a sentence: C++ is powerful!
Entered sentence: C++ is powerful!

				
			

String Functions in C++

Common functions for manipulating std::string objects include:
  • length()
  • swap()
  • size()
  • resize()
  • find()
  • push_back()
  • pop_back()
  • clear()

Example of Basic String Functions:

				
					Length: 9
After push_back: Developers
After pop_back: Developer
After clear: String is empty

				
			

Iterators for String Traversal

C++ provides iterators for traversing strings, including forward and reverse iterators.

Example:

				
					#include <iostream>
using namespace std;

int main() {
    string s = "Coding";

    cout << "Forward: ";
    for (auto itr = s.begin(); itr != s.end(); ++itr) {
        cout << *itr;
    }
    cout << endl;

    cout << "Reverse: ";
    for (auto ritr = s.rbegin(); ritr != s.rend(); ++ritr) {
        cout << *ritr;
    }
    cout << endl;

    return 0;
}

				
			

Output:

				
					Forward: Coding
Reverse: gnidoC

				
			

String Capacity Functions

Capacity functions control a string’s memory allocation.

1. length(): Returns the string’s length.
2. capacity():
Returns the memory allocated.
3. resize():
Changes the string size.
4. shrink_to_fit():
Reduces allocated capacity to the minimum size.

Example:

				
					#include <iostream>
using namespace std;

int main() {
    string s = "Optimize";

    cout << "Initial length: " << s.length() << endl;
    cout << "Initial capacity: " << s.capacity() << endl;

    s.resize(5);
    cout << "After resize: " << s << endl;

    s.shrink_to_fit();
    cout << "Capacity after shrink_to_fit: " << s.capacity() << endl;

    return 0;
}

				
			

Output:

				
					Initial length: 8
Initial capacity: 15
After resize: Optim
Capacity after shrink_to_fit: 5