Additional string operators

Slides:



Advertisements
Similar presentations
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Advertisements

Java Strings in 10 minutes
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Arrays.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
February 11, 2005 More Pointers Dynamic Memory Allocation.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Parameter Passing Mechanisms Reference Parameters Read § §
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Parameter Passing Mechanisms Reference Parameters § §
Exam Format  90 Total Points  60 Points Writing Programs  25 Points Tracing Code/Algorithms and determining results  5 Points Short Answer  Similar.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
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.
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.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
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.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Lecture 24: 12/3/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Strings.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Pointers What is the data type of pointer variables?
Basic concepts of C++ Presented by Prof. Satyajit De
Object-Oriented Programming Using C++
Student Data Score First Name Last Name ID GPA DOB Phone ...
Engineering 1020: Introduction to Programming Fall 2018
Chapter 2 – Getting Started
CS 2308 Exam I Review.
CS 1430: Programming in C++.
Summary of what we learned so far
Chapter 8: The string Type
CS 2308 Exam I Review.
Strings A collection of characters taken as a set:
Announcements 3rd homework is due this week Wednesday (March 15)
Chapter 2: Introduction to C++.
String.
A First Program.
Programming Strings.
character manipulation
Classes and Objects Systems Programming.
Presentation transcript:

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