Announcements HW1 is due TODAY.

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

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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Working with Strings Lecture 2 Hartmut Kaiser
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Announcements HW1 grades are announced at SUCourse You may see Belal Amro at his office hour for homework grades at FENS 2014 on Wednesday 10:40-12:30.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Announcements Final NEXT WEEK (August 13 th Thursday at 16:00) Recitations will be held on August 12 th Wednesday We will solve sample final questions.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
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.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
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++
String Class Mohamed Shehata 1020: Introduction to Programming.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Project 3: Ticket Printer
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Strings.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Session 2 Basics of PHP.
Chapter Topics The Basics of a C++ Program Data Types
Topic Pre-processor cout To output a message.
Chapter 2: Introduction to C++
Announcements Midterm2 Grades to be announced THIS WEEK
Announcements Final Exam will be on August 19 at 16:00
Basic Elements of C++.
Objectives Identify the built-in data types in C++
Working with Strings Lecture 2 Hartmut Kaiser
Announcements General rules about homeworks
Working with Forms and Regular Expressions
Announcements 2nd homework is due this week Wednesday (October 18)
Chapter 2: Introduction to C++
– Introduction to Object Technology
Basic Elements of C++ Chapter 2.
CISC101 Reminders Quiz 2 this week.
Engineering 1020: Introduction to Programming Fall 2018
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Announcements General rules about homeworks
Announcements General rules about homeworks
Announcements Midterm2 Grades to be announced NEXT Monday
Summary of what we learned so far
Working with Strings Lecture 2 Hartmut Kaiser
Announcements 3rd homework is due this week Wednesday (March 15)
Introduction to C++ Programming
Announcements Homework 1 will be assigned this week,
Announcements Final will be NEXT WEEK on August 17
Chapter 2: Introduction to C++.
Summary of what we learned yesterday
Introduction to Computer Science
Announcements Homework 1 will be assigned this week,
Announcements General rules about homeworks
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Today’s Objectives 28-Jun-2006 Announcements
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
Presentation transcript:

Announcements HW1 is due TODAY. New (HW2) will be assigned this week, due NEXT WEEK About strings and loops Common Submission Mistakes In HW1, some students submitted empty/wrong files Make sure that you send the cpp file; otherwise we cannot grade Please submit the required files only, not the entire project folder 7z, rar and tar format is not allowed for compression; please use zip Do not use blanks, Turkish characters, special symbols in the filenames Only English alphabet letters, digits and underscore are allowed General rules about homeworks Use of global variables (variables defined outside of functions) prohibited No abrupt program termination in the middle of the program. Modularity and code duplication are important Code duplication must be avoided Please write comments to your programs.

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;