Transcript String
C++ Data Type String
• A string is a sequence of characters
enclosed in double quotes.
• Examples of strings:
• “Hello” “CIS 260” “Students”
• The empty string (null string) contains no
characters and written as “”
C++ Data Type String
• string is not a built-in (standard) data type.
• string is a programmer-defined data type.
• It is provided in the C++ standard library.
• String operations are:
Comparing two string values
Searching a string for a particular character
value.
Joining one string to another
What is a Named Constant?
• A named constant is a location in memory that
we can refer to by an identifier, and in which a
data value that cannot be changed is stored.
• Examples:
const int CHEESE = 106;
const string star = “****”;
const float max_hours = 40.0;
String: How to declare and assign values
string str;
string Name;
str = “Hello”;
Name = “Cynthia”;
String Concatenation (+)
• Concatenation is a binary operation that
uses the + operator.
• At least one of the operands must be a string
variable or named constant – the other
operand can be string type or char type.
Concatenation Example
const string WHEN = “Tomorrow”;
const char EXCLAMATION = ‘!’;
string message1;
string message2;
message1 = “Welcome to”;
message2 = “CIS 260”;
message1 = message1 + message2 +
WHEN + EXCLAMATION
C++ program using string
#include <iostream>
#include <string>
using namespace std;
const string FIRST = "Herman";
const string LAST = "Smith";
const char MIDDLE = 'G';
// Person's first name
// Person's last name
// Person's middle initial
C++ program using string
int main()
{
string firstLast; // Name in first-last format
string lastFirst; // Name in last-first format
firstLast = FIRST + " " + LAST;
cout << "Name in first-last format is " << firstLast << endl;
lastFirst = LAST + ", " + FIRST + ", ";
cout << "Name in last-first-initial format is ";
cout << lastFirst << MIDDLE << '.' << endl;
return 0;
}
Output
Name in first-last format is Herman Smith
Name in last-first-initial format is Smith, Herman, G.
Namespace
The header file declares all its identifiers to be in a namespace
called std.
Namespace std
{
declaration of variables, data types, and so forth
}
Additional string operators
•
•
•
•
length
size
find
substr
The “length” function
If we apply length function to a string, it will return the number
of characters in that string.
Examples:
string firstName;
string lastname;
firstName = “Cynthia”;
cout << firstName . length() << endl; // prints 7
lastName = “Lokker”;
Cout << lastName . Length() << endl; // prints 6
Error message
If you forget to use dot notation and write simply:
length()
You will get a syntax error message:
“undeclared identifier.”
Compiler will think you are trying to call an ordinary function,
named length without declaration.
Compiler will not recognize that you are trying to call the length
function associated with the string type.
The find function
• We use “find” function to find the first occurrence of a particular
Substring.
• The function “find” returns an integer to indicate the position of
the substring in the searching string.
Examples:
string str;
str = “Introduction to programming”;
Function calls
str . find(“tion”)
str . find(“Intro”)
str . find(“prog”)
Str . Find(“or”)
Value returned by function
8
0
16
string::npos, a largest
possible value
The substr function
• We use the “substr” function to get a particular substring of a string.
• The “substr” function has two parameters:
first parameter gives the position of the first character of the
substring in the string
second parameter gives the length of the substring.
Examples:
string str;
str = “Introduction to programming”;
Function call
str . substr(0, 12)
str . substr(8, 7)
str . substr(40, 10)
Value returned by the function
Introduction
tion to
None. Program terminates
with an execution error