Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 “”

2 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

3 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;

4 String: How to declare and assign values string str; string Name; str = “Hello”; Name = “Cynthia”;

5 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.

6 Concatenation Example const string WHEN = “Tomorrow”; const char EXCLAMATION = ‘!’; string message1; string message2; message1 = “Welcome to”; message2 = “CIS 260”; message1 = message1 + message2 + WHEN + EXCLAMATION

7 C++ program using string #include using namespace std; const string FIRST = "Herman"; // Person's first name const string LAST = "Smith"; // Person's last name const char MIDDLE = 'G'; // Person's middle initial

8 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; }

9 Output Name in first-last format is Herman Smith Name in last-first-initial format is Smith, Herman, G.

10 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 }

11 Additional string operators length size find substr

12 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

13 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.

14 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 Value returned by function str. find(“tion”) 8 str. find(“Intro”) 0 str. find(“prog”) 16 Str. Find(“or”) string::npos, a largest possible value

15 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 Value returned by the function str. substr(0, 12) Introduction str. substr(8, 7) tion to str. substr(40, 10) None. Program terminates with an execution error


Download ppt "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."

Similar presentations


Ads by Google