String Type

In C++, the string class is part of the Standard Library, and it provides a convenient way to work with sequences of characters. The string class is defined in the <string> header. Unlike C-style strings, character arrays terminated by a null character ('\0'), C++ string objects automatically manage their memory and provide many useful member functions.

Here's an example of how to use a C++ string:

Demo 1

// Module 1 Demo // Author: Sam Student // Created on 5-19-2024. // Demonstrate C++ String Data Type #include <iostream> #include <string> using namespace std; int main() { // create a string variable string myOutput = "Welcome to C++ Programing"; cout << "The length of the string: " << myOutput << " is " << myOutput.length() << endl; cout << "The character at position 0 is " << myOutput.at(0) << endl; cout << "The character at position 11 is " << myOutput.at(11) << endl; cout << "End of Program!\n"; return 0; }

This C++ program demonstrates the usage of strings in C++ and showcases some of the string member functions.

Here's a step-by-step explanation:

  1. Include Statements: The program includes the necessary header file iostream which provides input/output stream functionalities.

  2. Namespace: The using namespace std; statement is used to avoid having to prefix standard library elements with std:: later in the code.

  3. Variable Declaration: The program declares a string variable myOutput and initializes it with the value "Welcome to C++ Programming". This string represents a message to be displayed.

  4. Output Length: The program uses the length() member function of the string class to determine the length of the string myOutput. It then prints "The length of the string: " followed by the value of myOutput and " is " followed by the length of the string using the cout statement.

  5. Access Character: The program uses the at() member function of the string class to access the character at position 0 in the string myOutput. It then prints "The character at position 0 is " followed by the character using the cout statement.

  6. End of Program: The program prints "End of Program!" to signify the end of its execution, and the main function returns 0, indicating that the program has finished successfully.

Overall, this program demonstrates the basic usage of strings in C++ and how to access their length and individual characters.


Demo 2

// Demo of C++ string and character #include <iostream> #include <string> using namespace std; int main() { // Declare and initialize a string string myString = "Hello, World!"; // Access a character in the string char firstChar = myString[0]; // Modify a character in the string myString[0] = 'h'; // Get the length of the string size_t length = myString.length(); // Concatenate strings string anotherString = " This is C++."; string combinedString = myString + anotherString; // Print the modified string and its length cout << "Modified string: " << myString << endl; cout << "Length: " << length << endl; cout << "Combined string: " << combinedString << endl; return 0; }

This C++ program demonstrates various operations with strings and characters. Here's a step-by-step explanation:

  1. Include Statements: The program includes the necessary header files iostream and string to provide input/output stream functionalities and string operations, respectively.

  2. Namespace: The using namespace std; statement is used to avoid having to prefix standard library elements with std:: later in the code.

  3. Variable Declaration: The program declares and initializes a string variable my_string with the value "Hello, World!". This string represents a message.

  4. Accessing a Character: The program uses square brackets ([]) to access an individual character in the string myString. It assigns the first character of myString to the variable firstChar.

  5. Modifying a Character: The program uses square brackets ([]) to modify an individual character in the string myString. It changes the first character of myString to lowercase 'h'.

  6. Getting the Length: The program uses the length() member function of the string class to get the length of the string myString. It assigns the length to the variable length.

  7. Concatenating Strings: The program declares and initializes a new string variable anotherString with the value " This is C++.". It then uses the + operator to concatenate myString and anotherString, assigning the result to combinedString.

  8. Output: The program uses the cout statement to print the modified string myString, followed by the variable length to display the length of myString. It also prints the concatenated string combinedString.

  9. End of Program: The program reaches the end, and the main function returns 0, indicating that the program has finished successfully.

Overall, this program demonstrates operations with strings, including accessing characters, modifying characters, obtaining the length, and concatenating strings.

C++ strings also support many global functions for string manipulation, such as getline(), stoi(), stod(), to_string(), etc. These functions can be found in the <string> headers.

 

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark