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:
Include Statements: The program includes the necessary header file
iostream
which provides input/output stream functionalities.Namespace: The
using namespace std;
statement is used to avoid having to prefix standard library elements withstd::
later in the code.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.Output Length: The program uses the
length()
member function of the string class to determine the length of the stringmyOutput
. It then prints "The length of the string: " followed by the value ofmyOutput
and " is " followed by the length of the string using thecout
statement.Access Character: The program uses the
at()
member function of the string class to access the character at position 0 in the stringmyOutput
. It then prints "The character at position 0 is " followed by the character using thecout
statement.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:
Include Statements: The program includes the necessary header files
iostream
andstring
to provide input/output stream functionalities and string operations, respectively.Namespace: The
using namespace std;
statement is used to avoid having to prefix standard library elements withstd::
later in the code.Variable Declaration: The program declares and initializes a string variable
my_string
with the value "Hello, World!". This string represents a message.Accessing a Character: The program uses square brackets (
[]
) to access an individual character in the stringmyString
. It assigns the first character ofmyString
to the variablefirstChar
.Modifying a Character: The program uses square brackets (
[]
) to modify an individual character in the stringmyString
. It changes the first character ofmyString
to lowercase'h'
.Getting the Length: The program uses the
length()
member function of the string class to get the length of the stringmyString
. It assigns the length to the variablelength
.Concatenating Strings: The program declares and initializes a new string variable
anotherString
with the value " This is C++.". It then uses the+
operator to concatenatemyString
andanotherString
, assigning the result tocombinedString
.Output: The program uses the
cout
statement to print the modified stringmyString
, followed by the variablelength
to display the length ofmyString
. It also prints the concatenated stringcombinedString
.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