1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Classes and Data Abstraction Lecture 9. Objects Models of things in the real world Defined in classes  Class name is the object name Example: Library.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
1 Week 1304/07/2005Course ISM3230Dr. Simon Qiu  Learn how to create and manipulate enumeration type  Become aware of the typedef statement  Learn about.
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.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 9: Arrays and Strings
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
C++ Programming:. Program Design Including
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
Numeric Types, Expressions, and Output ROBERT REAVES.
Chapter 8 Arrays and Strings
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
1 C++ Syntax and Semantics, and the Program Development Process.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Programming: Basic Elements of C++.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
First steps Jordi Cortadella Department of Computer Science.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
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.
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 
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Functions & Strings CIS Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer.
Program Input and the Software Design Process ROBERT REAVES.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Lecture 24: 12/3/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 8: The string Type
Chapter 3: Input/Output
Wednesday 09/23/13.
Chapter 4: Control Structures I (Selection)
Engineering Problem Solving with C++, Etter
Strings Skill Area 313 Part C
Programming Strings.
character manipulation
Presentation transcript:

1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006

2 string Data Type Programmer-defined type supplied in standard library A string is a sequence of zero or more characters Enclosed in double quotation marks Example: “ William Jacob ” “ Mickey ” Null (empty string): a string with no characters Example: “” Each character has relative position in string Position of first character is 0, the position of the second is 1, and so on For example, in the string “ William Jacob ”, the first character, 'W', in name is in position 0, the second character, 'i', is in position 1, and so on Length: number of characters in string

3 The string Type To use the data type string, the program must include the header file The statement: string name = "William Jacob"; declares name to be a string variable and also initializes name to "William Jacob “ The variable name is capable of storing any size string

4 I/O operations on the string Type An input stream variable (cin) and extraction operator >> can read a string into a variable of the data type string Extraction operator Skips any leading whitespace characters and reading stops at a whitespace character Should not be used to read strings with blanks The function getline Reads until end of the current line Should be used to read strings with blanks

5 #include using namespace std; int main() { string mystring; cin>>mystring; cout<<mystring<<endl; return 0; } #include using namespace std; int main() { string mystring; getline(cin, mystring); cout<<mystring<<endl; return 0; } I/O operations on the string Type – Example Assume the input is: Break Back Mountain Output: BreakOutput: Break Back Mountain

6 Relational operators on string Type: Comparing string Types Relational operators can be applied to strings Strings are compared character by character, starting with the first character Comparison continues until either a mismatch is found or all characters are found equal If two strings of different lengths are compared and the comparison is equal to the last character of the shorter string The shorter string is less than the larger string

7 string Comparison Example Suppose we have the following declarations: string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill";

8 Other operators on the string Type Binary operator + allows the string concatenation operation For example, If str1 = "Sunny", the following statement stores the string "Sunny Day" into str2: str2 = str1 + " Day"; The array subscript operator [] allows to access an individual character within a string For example, If str1 = “ Hello there", the following statement replaces the character t with the character T: str1[6] = ‘ T ’ ;

9 #include using namespace std; int main() { string str1, str2, str3, str4; //Line 1 str1 = "Hello There";//Line 2 cout << "Line 3: str1 = " << str1 << endl; //Line 3 str2 = str1;//Line 4 cout << "Line 5: str2 = " << str2 << endl; //Line 5 str1 = "Sunny";//Line 6 str2 = str1 + " Day";//Line 7 cout << "Line 8: str2 = " << str2 << endl; //Line 8 str1 = "Hello";//Line 9 str2 = "There";//Line 10 str3 = str1 + " " + str2;//Line 11 cout << "Line 12: str3 = " << str3 << endl; //Line 12 str3 = str1 + ' ' + str2;//Line 13 cout << "Line 14: str3 = " << str3 << endl;//Line 14 str1 = str1 + " Mickey";//Line 15 cout << "Line 16: str1 = " << str1 << endl; //Line 16 str1 = "Hello there";//Line 17 cout << "Line 18: str1[6] = " << str1[6] << endl;//Line 18 str1[6] = 'T';//Line 19 cout << "Line 20: str1 = " << str1 << endl;//Line 20 return 0; } Binary operator + and array subscript operator [] – Example Line 3: str1 = Hello There Output: Line 5: str2 = Hello There Line 8: str2 = Sunny Day Line 12: str3 = Hello There Line 14: str3 = Hello There Line 16: str1 = Hello Mickey Line 18: str1[6] = t Line 20: str1 = Hello There

10 Some functions on string length size find substr swap

11 length Function Length returns the number of characters currently in the string The syntax to call the length function is: strVar.length() where strVar is variable of the type string length has no arguments length returns an unsigned integer The value returned can be stored in an integer variable

12 length function – Example Outputs 9 #include using namespace std; int main() { string firstName; string name; firstName = "Elizabeth"; name = firstName + " Jones"; cout << firstName.length() << endl; cout << name.length() << endl; return 0; } Outputs 15

13 size Function The function size is same as the function length Both functions return the same value The syntax to call the function size is: strVar.size() where strVar is variable of the type string As in the case of the function length, the function size has no arguments

14 size function – Example Outputs 9 #include using namespace std; int main() { string firstName; string name; firstName = "Elizabeth"; name = firstName + " Jones"; cout << firstName.size() << endl; cout << name.size() << endl; return 0; } Outputs 15

15 find Function find searches a string for the first occurrence of a particular substring Returns an unsigned integer value of type string::size_type giving the result of the search The syntax to call the function find is: strVar.find(strExp) or strVar.find(strExp, pos) where strVar is a string variable and strExp is a string expression evaluating to a string The string expression, strExp, can also be a character If successful, find returns the position in strVar where the match begins For the search to be successful the match must be exact If unsuccessful, find returns the special value string::npos ( “ not a position within the string ” )

16 find function – Example Outputs 11 //Example find function #include using namespace std; int main() { string sentence, str;//Line 1 string::size_type position;//Line 2 sentence = "Outside it is cloudy and warm."; //Line 3 str = "cloudy";//Line 4 cout (sentence.find("is")) << endl; //Line 5 cout (sentence.find('s')) << endl; //Line 6 cout (sentence.find('o')) << endl; //Line 7 cout (sentence.find(str)) << endl; //Line 8 cout (sentence.find("the")) << endl; //Line 9 cout (sentence.find('i', 6)) << endl; //Line 10 position = sentence.find("warm");//Line 11 cout (position) << endl;//Line 12 return 0; } Outputs 3 Outputs 16 Outputs 14 Outputs Outputs 8 Outputs 25

17 substr Function substr returns a particular substring of a string The syntax to call the function substr is: strVar.substr(expr1,expr2) where expr1 and expr2 are expressions evaluating to unsigned integers The expression expr1 specifies a position within the string (starting position of the substring) The expression expr2 specifies the length of the substring to be returned

18 substr function – Example Outputs: It is //Example substr function #include using namespace std; int main() { string sentence;//Line 1 string str;//Line 2 sentence = "It is cloudy and warm."; //Line 3 cout << sentence.substr(0, 5) << endl;//Line 4 cout << sentence.substr(6, 6) << endl;//Line 5 cout << sentence.substr(6, 16) << endl;//Line 6 cout << sentence.substr(17, 10) << endl;//Line 7 cout << sentence.substr(3, 6) << endl;//Line 8 str = sentence.substr(0, 8);//Line 9 cout << str << endl;//Line 10 str = sentence.substr(2, 10);//Line 11 cout << str << endl;//Line 12 return 0; } Outputs: cloudy Outputs: cloudy and warm. Outputs: warm. Outputs: is clo Outputs: It is cl Outputs: is cloudy

19 swap Function swap interchanges the contents of two string variables The syntax to use the function swap is strVar1.swap(strVar2); where strVar1 and strVar2 are string variables Suppose you have the following statements: string str1 = "Warm"; string str2 = "Cold"; After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "Warm"

20 swap function – Example //Example substr function #include using namespace std; int main() { string string1, string2;//Line 1 string1 = "Warm";//Line 2 string2 = "Cold";//Line 3 cout << "string1: " << string1 << " string2:" << string2 << endl;//Line 4 string1.swap(string2);//Line 5 cout << "string1: " << string1 << " string2:" << string2 << endl;//Line 6 return 0; }

21 End of lecture 18 Thank you!