Summary of what we learned so far

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

Java Programming Strings Chapter 7.
Chapter 10.
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.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Working with Strings Lecture 2 Hartmut Kaiser
Numeric Types, Expressions, and Output ROBERT REAVES.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
A Computer Science Tapestry 1 Announcements l Midterm 1 is on November 25, 2006, 10:40 – 12:20 l Homework 3 is due November 8, 2006, 21:00 ä Robot application.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
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 Character Strings (Cstrings) Reference: CS215 textbook pages
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.
Data Structures: CSCI Chapter 1 Data Structures: CSCI Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
1 CSC 222: Computer Programming II Spring 2004  classes and objects  abstract data types, classes and objects  using existing classes, #include, member.
STL string class Programming Team 2/15/2006. string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char*
Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
13. string Operations Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Accessing String Elements.
Strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Topic Pre-processor cout To output a message.
Chapter 2: Introduction to C++
Working with Strings Lecture 2 Hartmut Kaiser
CMPT 120 Topic: Python strings.
Strings Part 1 Taken from notes by Dr. Neil Moore
Announcements 2nd homework is due this week Wednesday (October 18)
– Introduction to Object Technology
Lecture 4 Using Classes Richard Gesick.
CISC101 Reminders Quiz 2 this week.
Engineering 1020: Introduction to Programming Fall 2018
Chapter 7: Strings and Characters
Announcements General rules about homeworks
Announcements General rules about homeworks
C++ STRINGS string is part of the Standard C++ Library
Learning Objectives String Class.
Working with Strings Lecture 2 Hartmut Kaiser
Announcements 3rd homework is due this week Wednesday (March 15)
Class string and String Stream Processing
Wednesday 09/23/13.
Representation and Manipulation
CS1110 Today: collections.
Introduction to Computer Science
Additional string operators
Announcements General rules about homeworks
Today’s Objectives 28-Jun-2006 Announcements
Topic 25 - more array algorithms
character manipulation
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
What is a String? String s = "compsci"; s c o m p s i
Announcements HW1 is due TODAY.
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

Summary of what we learned so far Classes How to use classes/objects Header (.h) and implementation (.cpp) files Today’s topics String class Member functions: length, substr, find/rfind Loops we will start and finish next week.

string as a class string is a class string variables are objects, they’re instances of the class A class is a collection of members that have common attributes strings share some properties, but have different values A string is a sequence of characters first char is at position 0 first index is 0 last valid index (position of the last character): string’s length -1 Constructors without initialization string mystr, s; with initialization string s = "Hello World"; string otherstring = s; or string otherstring(s); You may use functions and operators that return strings in initialization string s = "Hello" + "World"; //concatenation operator

string member functions The function length() returns the number of characters string s = "hello"; int len = s.length(); // value of len is 5 s = ""; // s is null string len = s.length(); // value of len is 0 Member functions are applied to objects using dot notation Cannot use length() without an object to apply it to Not valid int x = length(s); Not valid int x = string.length(); Valid? double y = sqrt(s.length());

string member functions int length() // postcondition: returns the number of characters string substr(int pos, int len) // precondition: 0 <= pos, and pos < length of the string object // postcondition: returns substring of len characters beginning at position // pos. Returns as many characters as possible if len is too large, but // causes error if pos is out of range (>= length of the string object) int find(string s) // postcondition: returns first position/index at which substring s begins in // string object– returns string::npos if s does not occur More details in “how to C” in Tapestry

Extracting substrings A substring is part of a string, substrings can be extracted from a string using member function substr string s = "theater"; int len = s.length(); // value of len is 7 string t = s.substr(0,3); // t is "the", s is “theater” t = s.substr(1,4); // t is now “heat” s = s.substr(3,3); // s is “ate” t is still “heat” s = “cinema”; t = s.substr(4,8); // t is “ma” t = s.substr(8,2); // run-time error See strdemo.cpp which is a sample program that uses length and substr functions t h e a t e r 1 2 3 4 5 6 c i n e m a 1 2 3 4 5

Finding substrings within strings: find and rfind String member function find looks for an occurrence of one string (which is a parameter) in another (which is the object string), returns position of the start of the first occurrence If no occurrence, then string::npos is returned largest unsigned integer (4,294,967,295) string s = "I am the eggman, he is too"; int k = s.find("I"); // k is 0 k = s.find("he"); // k is 6 k = s.find("egg"); // k is 9 k = s.find("a"); // k is 2 cout << s.find("cat"); // output is 4294967295 k = s.find("cat"); // k is –1 since it is signed, // we should use unsigned int rfind is same as find, but searches backwards, returns the last occurrence k = s.rfind("he"); // k is 17 k = s.rfind("egg"); // k is 9 See strfind.cpp which is a sample program on find function

find and rfind with 2 parameters There is another version of find and rfind that takes two parameters First parameter is still a string (the search string) Second parameter is an integer (an index value) In this version, searching starts from the character for which the index is the second parameter of find or rfind Useful when you need to search a string not from the endpoint, but from somewhere in the middle string s = "I am the eggman, he is too"; int k; k = s.find("a"); // k is 2 k = s.find("a",5); // k is 13 k = s.rfind("he"); // k is 17 k = s.rfind("he", 15); // k is 6

More on strings You can append one string to the end of another using operator + string s = "cs201"; s = s + " is easy"; // s is now "cs201 is easy" You can change or extract one character of a string using at member function parameter of at must be between 0 and string’s length - 1, otherwise a run-time error occurs string s = "Hello World"; s.at(4) = '!'; // s is now "Hell! World" cout << s.at(1) ; // displays e on screen s.at(20) = 'a'; // run-time error

More on strings We already know how to compare strings See “How to C” insert and replace functions String functions and operators we have seen so far are from standard C++ library Tapestry also has some classes and utility functions we will see and use them in time strutils.h and strutils.cpp Tapestry string utilities include strutils.h at the beginning of the program add strutils.cpp to your project see “How to G” (page 776)

Example (See stringremove.cpp) Write a function that takes two string parameters (say s1 and s2). Function removes first occurrence of s2 in s1 and returns the resulting string. In the main program, test the function by removing some input strings from “Today is Monday” See program in next slide

Example (See stringremove.cpp) string remove(string s1, string s2) // post: removes first occurrence of s2 from s1 and returns the // resulting string. // Returns s1 if s2 does not occur any. { unsigned int location; string temp = s1; location = s1.find(s2); if (location != string::npos) temp = s1.substr(0, location) + s1.substr(location+s2.length(), s1.length()); } return temp;