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.

Slides:



Advertisements
Similar presentations
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Advertisements

1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
CS150 Introduction to Computer Science 1
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 2: Introduction to C++.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Input & Output: Console
Numeric Types, Expressions, and Output ROBERT REAVES.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
IXA 1234: C++ PROGRAMMING CHAPTER 2: PROGRAM STRUCTURE.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
String Class Mohamed Shehata 1020: Introduction to Programming.
1 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.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Topics – Programs Composed of Several Functions – Syntax Templates – Legal C++
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Lecture 2A Data Types Richard Gesick
Documentation Need to have documentation in all programs
Chapter 2 Topics Programs Composed of Several Functions
Object-Oriented Programming Using C++
Chapter 2 – Getting Started
2.1 Parts of a C++ Program.
Pointers, Dynamic Data, and Reference Types
Wednesday 09/23/13.
Introduction to C++ Programming
CPS120: Introduction to Computer Science
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++, Etter
Chapter 2: Introduction to C++.
Additional string operators
Strings Skill Area 313 Part C
Programming Strings.
Presentation transcript:

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

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

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