5 Pages

lecIV.2-notes

Course: CIS 1, Fall 2009
School: CUNY Baruch
Rating:
 
 
 
 
 

Word Count: 1373

Document Preview

strings today: what are strings and why to use them reading: textbook chapter 8 what are strings a string in C++ is one of a special kind of data type called a class we will talk more about classes in detail at the end of the term but note that we have already used two classes when we covered les: the ifstream class and the ofstream class a class is a compound data type, unlike the simple, native data types...

Register Now

Unformatted Document Excerpt

Coursehero >> New York >> CUNY Baruch >> CIS 1

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
strings today: what are strings and why to use them reading: textbook chapter 8 what are strings a string in C++ is one of a special kind of data type called a class we will talk more about classes in detail at the end of the term but note that we have already used two classes when we covered les: the ifstream class and the ofstream class a class is a compound data type, unlike the simple, native data types weve already discussed (e.g., int, char, bool, double, etc) a class has members: it has data elds and functions cis1.5-spring2008-azhar-lecIV.2 1 cis1.5-spring2008-azhar-lecIV.2 2 strings: declaring and initializing strings are declared like this: string s where s is a variable whose data type is a string you can set the value of the string using the assignment operator and double quotes ("): s = "hello"; NOTE that you use single quotes for char values and double quotes for string values: char c = A; string s = "hello"; ALSO NOTE that when you use the string class, you also need to include the string header le: #include <string> using namespace std; strings: output we have already used strings for output, e.g.: cout << "hello" << endl; but we have not yet output variables that were declared as strings but we can... its just like this: #include <iostream> #include <string> using namespace std; int main() { string s = "hello"; cout << s << endl; } // end of main() cis1.5-spring2008-azhar-lecIV.2 3 cis1.5-spring2008-azhar-lecIV.2 4 strings: input there are two ways to read input values from the keyboard (or from a le) into a string variable: (1) using the >> operator (2) using the getline() function the rst way, using the >> operator, will only read until the rst whitespace character is read (the term whitespace refers to characters like blank spaces, tabs and newlines) when reading a string variable using the >> operator, the input will stop as soon as the rst whitespace character is read example (on the next page): #include <iostream> #include <string> using namespace std; int main() { string s; cout << "please enter your name:"; cin >> s; cout << "s = " << s << endl; } // end of main() if the user enters david ortiz when the program asks please enter your name: then the value of s will be david cis1.5-spring2008-azhar-lecIV.2 5 cis1.5-spring2008-azhar-lecIV.2 6 HOWEVER, when reading a string variable using the getline() function, the input will stop as soon as the rst newline character is read (i.e., the user hits the ENTER key on the keyboard), e.g.: #include <iostream> #include <string> using namespace std; int main() { string s; cout << "please enter your name:"; getline( cin, s ); cout << "s = " << s << endl; } // end of main() here, if the user enters david ortiz when the program asks please enter your name: then the value of s will be david ortiz note that the getline() function can optionally take a third argument, a delimiter this would be for cases where you wanted to stop the input not at whitespace or at a newline, but at some other character called the delimiter suppose you wanted to enter several commands for a robot to follow using one string, like this: forward|wait|backward|turn left|stop you could do this using the | (vertical bar) character as the delimiter and the getline() function with three arguments, e.g.: string s; cout << "enter some commands: "; getline( cin, s, | ); If the user entered forward|backward when prompted by the above program, the value of s would be forward, since the input would stop at the delimiter (|). You would have to call getline( cin, s, | ); again to read the next command (e.g., wait). cis1.5-spring2008-azhar-lecIV.2 7 cis1.5-spring2008-azhar-lecIV.2 8 strings: operators there are several operators that work with strings the plus sign (+) is the concatenation operator, e.g.: string s1, s2, s3; s1 = "david "; s2 = "ortiz"; s3 = s1 + s2; After the above code fragment, the value of s3 will be david ortiz the comparison operators also work with strings (==, <, <=, >, >=) the double equals sign (==) compares the value of two strings and returns true if they are the same, e.g.: string s1, s2, s3; bool a1, a2; s1 = "david "; s2 = "ortiz"; s3 = "david "; a1 ( = s1 == s2 ); a2 = ( s1 == s3 ); After the above code fragment: the value of a1 will be false and the value of a2 will be true cis1.5-spring2008-azhar-lecIV.2 9 cis1.5-spring2008-azhar-lecIV.2 10 the inequality operators (<, <=, >, >=) perform a lexical comparison between two strings a lexical comparison is like checking if two strings are in alphabetical order: one is less than the other if it comes before the other alphabetically EXCEPT, the lexical comparison is case sensitive and uses the ASCII table, which means that all the upper case letters (A..Z) come before (are less than) all the lower case letters (a..z), e.g.: string s1, s2, s3; bool a1, a2; s1 = "ABC"; s2 = "DEF"; s3 = "abc "; a1 = ( s1 < s2 ); a2 = ( s3 < s2 ); After the above code fragment: the value of a1 will be true because "ABC" < "DEF" and the value of a2 will be false because "abc" > "DEF" strings: indexes a string is like an array of char so you can use the index of the individual characters of the string just like you can use the indexes of the individual elements of an array, like the arrays of ints you created for the last homework assignment if you have: string s = then: s[0] is s[1] is s[2] is s[3] is s[4] is "ortiz"; assigned the assigned the assigned the assigned the assigned the value value value value value o (the letter oh) r t i z you can also use the member function at() to nd the value of an individual character of a string e.g., instead of using s[3], you can use s.at(3) cis1.5-spring2008-azhar-lecIV.2 11 cis1.5-spring2008-azhar-lecIV.2 12 strings: length if you have: string s = "ortiz"; then the length of the string is 5 there are two member functions of the string class that will tell you the length of a string: length() and size() (they do the same thing) you call them like this: string s1; int n1, n2; s1 = "ortiz"; n1 = s1.length(); n2 = s1.size(); After this code fragment, the value of n1 will be 5 and so will the value of n2 strings: searching the find() member function is used to locate a substring within a primary string the function returns the value of the index in the primary string at which the substring starts, if the substring exists in the primary string; or else the function returns the constant string::npos for example: string s1 = "david ortiz"; int n1, n2; n1 = s1.find( "avid", 0 ); n2 = s1.find( "ask", 0 ); After the above code fragment: the value of n1 will be 1 the value of n2 will be string::npos the rst argument to the find() function is the substring to search for the second ar...

Textbooks related to the document above:
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

CUNY Baruch - CIS - 1
today: math operations and function arguments random numbers math operators data type conversion function argumentsrandom numbers computers can generate random numbers, which is like picking a number by rolling dice there are two steps necess
CUNY Baruch - CIS - 1
today: functions what are functions and why to use them library and programmer-defined functions parameters and return values reading: textbook chapter 5, sections 1-4 modularityadvantages of functions we can divide up a program into small,
CUNY Baruch - CIS - 1
today: arrays what are arrays and why to use them integer arrays reading: textbook chapter 7, sections 3-4arrays arrays are used to hold sets of related types of data the data could be integers or doubles or booleans the data could also be ch
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ fall 2008 lecture # I.2 topics: unix fundamentalswhat is UNIX? Unix is an operating system (like Windows) which means it is a program that runs on a computer that makes it possible for you to use t
CUNY Baruch - CIS - 1
cis1.5-spring2008-azhar, lab IIIinstructions This assignment will be worth 9 points. Both parts together are due on Wednesday March 26 and must be submitted by email (as below). Follow these emailing instructions: 1. Create a mail message address
CUNY Baruch - CIS - 1
today: sorting algorithms blort sort selection sort insertion sort bubble sort FINAL EXAM: Thursday, May 22nd (1pm - 3pm) (place to be announced.)sorting sorting is one of the classic tasks done in computer programming the basic idea with s
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ fall 2008 lecture # V.1 topics: arrays pointers arrays of objects resources: some of this lecture is covered in parts of Pohl, chapter 3arrays and pointers overview arrays and pointers are strong
CUNY Baruch - CIS - 10
Philosophy of Artificial Intelligence Paper 2 Due Tuesday, May 1 In this paper you will work out some ideas from section 6 of Daniel Dennett's &quot;Quining Qualia.&quot; Below I give you some very strong suggestions about how to proceed. (If you want to appro
CUNY Baruch - CIS - 15
cis15/summer2008/ozgelenC+ ReviewWrite a complete C+ program, including at least one comment for the main program and one for each function, as follows. Your program will emulate some aspects of a card game based on the game of Hearts. Its okay if
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment IIinstructions This is assignment for unit II. It is worth 10 points. It must be submitted by email (as below). Follow these emailing instructions: 1. Create a mail message addressed to ozgelen@sci.brooklyn.c
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment IVinstructions This is assignment for unit IV. It is worth 10 points. It must be submitted by email (as below). Follow these emailing instructions: 1. Create a mail message addressed to ozgelen@sci.brooklyn.c
CUNY Baruch - CIS - 15
Quick and Dirty EMACSNote that any ordinary character goes into the buffer (no insert is needed). For a more detailed emacs manual, check out the GNU emacs manual on-line at: http:/www.gnu.org/software/emacs/manual/Some necessary abbreviations:CM
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment III, part 2instructions This is the second part of the assignment for Unit III. It is worth 5 points. Follow these emailing instructions: 1. Create a mail message addressed to ozgelen@sci.brooklyn.cuny.edu with
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment III, part 1instructions This is the rst part of the assignment for Unit III. It is worth 5 points. Follow these emailing instructions: 1. Create a mail message addressed to ozgelen@sci.brooklyn.cuny.edu with th
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment I, part 1instructions This is the rst part of the assignment for unit I. The entire assignment will be worth 5 points. The rst part is worth 2 points. The second part is worth 3 points. Both parts must be su
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # VI.1 topics: recursion searchingrecursion recursion is defining something in terms of itself there are many examples in nature and in mathematics and in computer graphics,
CUNY Baruch - CIS - 15
cis15-summer2008 ozgelen, assignment I, part 2instructions This is the rst part of the assignment for unit I. The entire assignment will be worth 5 points. The rst part is worth 2 points. The second part is worth 3 points. Both parts must be su
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # V.2 topics: functions: parameters and arguments call by value vs call by reference namespaces generic pointers dynamic memory allocation resources: Pohl, chapter 3functions
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # V.1 topics: arrays pointers arrays of objects resources: some of this lecture is covered in parts of Pohl, chapter 3arrays review a data structure consisting of related elem
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # II.2 topics: ctors and dtors polymorphism and overloading friend classes, composition and derivation resources: Pohl, chapter 5 (mostly sections 5.1-5.3, 5.7, 5.10)ctors and
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ spring 2008 lecture # VII.1 topics: generic programming templates STL (standard template library) on-line reference: http:/www.cppreference.com/index.htmlgeneric programming methodology for enhan
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # I.1 introduction topics: (0) introduction to the course (1) to do (2) review of c+ instructor: Arif Tuna Ozgelen, ozgelen@sci.brooklyn.cuny.edu course web page: http:/www.sci.bro
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # IV.1 topics: inheritance composition of classes resources: Pohl, chapters 8 and 11an example consider the program patrol.cpp (posted on the class web page) this program mode
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment I, part 3 (optional)instructions This is an optional third part of the assignment for unit I. This part is worth 1 point of extra credit and can be submitted along with parts 1 and 2.1. enumerated data types
CUNY Baruch - CIS - 1
cis1.5-fall2008-ozgelen, lab Vinstructions This lab must be submitted by email (as below): 1. Create a mail message addressed to ozgelen@sci.brooklyn.cuny.edu with the subject line cis1.5. 2. Attach ONLY the .cpp files for each part, as outlined be
CUNY Baruch - CIS - 1
today: math operations and function arguments random numbers math operators data type conversion function arguments file operationsrandom numbers computers can generate &quot;random&quot; numbers, which is like picking a number by rolling dice there a
CUNY Baruch - CIS - 1
today: sorting algorithms blort sort selection sort insertion sort bubble sortsorting sorting is one of the classic tasks done in computer programming the basic idea with sorting is to rearrange the elements in an array so that they are in a
CUNY Baruch - CIS - 1
cis1.5-fall2008-ozgelen, lab II, part 2instructions This part of the lab is worth 5 points. Both parts must be submitted by email (as below): 1. Create a mail message addressed to ozgelen@sci.brooklyn.cuny.edu with the subject line cis1.5. 2. Atta
CUNY Baruch - CIS - 1
cis1.5 introduction to computing using c+ (legal applications) fall 2008 lecture # I.1 introductionintroduction to the course about this course introduction to computer programming using the C+ language uses legal applications as a context (i.e.
CUNY Baruch - CIS - 1
today: new topics: two-dimensional arrays switch statement review topics: constants string functions (see lecture IV.2 and textbook chapter 8) cctype functions (see lecture IV.2 and textbook chapter 8) In class, we followed a comprehensive ex
CUNY Baruch - CIS - 1
today: logical operations and control structures the if statement relational operators logical operators truth tables loops: the while and for statementsthe if branching statement we have already been using the if statement: void decreaseRais
CUNY Baruch - CIS - 1
today: strings what are strings and why to use them reading: textbook chapter 8what are strings a string in C+ is one of a special kind of data type called a class we will talk more about classes in detail at the end of the term but note that
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # I.2 topics: unix fundamentalswhat is UNIX? Unix is an operating system (like Windows). OS? - A program that coordinates and oversees the resources of a computer and provides a
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # III.1 topics: C-style strings input and output resources: Pohl, chapter 9C-style strings (1). storing multiple characters in a single variable data type is still char BUT i
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # II.1 topics: objects and class design command line arguments resources: Pohl, chapter 4aggregate data types class and struct struct comes from C class is new in C++ both a
CUNY Baruch - CIS - 1
today: functions what are functions and why to use them library and programmer-dened functions parameters and return values reading: textbook chapter 5, sections 1-4 modularityadvantages of functions we can divide up a program into small, un
CUNY Baruch - CIS - 1
today: arrays what are arrays and why to use them integer arrays reading: textbook chapter 7, sections 3-4arrays arrays are used to hold sets of related types of data the data could be integers or doubles or booleans the data could also be ch
CUNY Baruch - CS - 3157
MAKE(1L)LOCAL USER COMMANDSMAKE(1L)NAME make - GNU make utility to maintain groups of programs SYNOPSIS make [ -f _ a_ e_ i_ e ] [ option ] . target . m_ k_ f_ l_ WARNING This man page is an extract of the documentation of _ N_ _ a_ e G_ U m_ k
CUNY Baruch - CS - 1007
CS1007 lecture #4 notesthu 12 sep 2002 news data types and storage variables and assignment binary numbers and arithmetic ASCII Strings math operators increment and decrement operators reading: ch 2.5,2.7-2.12cs1007-fall2002-sklar-lect04
CUNY Baruch - CS - 1007
CS1007 lecture #21 notesthu 21 nov 2002 news vectors linear searching reading: ch 12.1-12.6cs1007-fall2002-sklar-lect21 1vectors (1).Java has a nice class which handles arrays dynamically: java.util.Vector the elements of a Vector can be
CUNY Baruch - CS - 1007
CS1007 lecture #6 notesthu 19 sep 2002 news homework #2 will be posted by midnight tonight homework #1 should be returned in recitation next week the while and for loops break and continue statements switch statement reading: ch 3.5-3.10 cs10
CUNY Baruch - CS - 3157
fork(2)fork(2)NAMEfork, fork1 - create a new processSYNOPSIS#include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; pid_t fork(void); pid_t fork1(void);MT-LEVELfork( ) is Async-Signal-SafeDESCRIPTIONfork( ) and fork1( ) cause creation of a new proce
CUNY Baruch - CS - 1007
CS1007 lecture #10 notestue 8 oct 2002 NEWS error quiz # 1, question 1d if you got the question WRONG, bring the quiz to class on thu 10 oct for a one-time regrade arrays (one-dimensional) nding array minimum and maximum sorting big-Oh 2-dimension
CUNY Baruch - CS - 1007
CS1007 lecture #15 notestue 29 oct 2002 news networks applets GUIs reading: ch 8.1-8.4cs1007-fall2002-sklar-lect15 1networks (1).two or more computers connected to each other networked computers can share information and resources, e.g.:
CUNY Baruch - CS - 3157
SED(1)User CommandsSED(1)NAME sed - a Stream EDitor SYNOPSIS sed [-n] [-V] [-quiet] [-silent] [-version] [-help] [-e script] [-expression=script] [-f script-file] [-file=script-file] [script-if-no-other-script] [file.] DESCRIPTION _ e_ is a str
CUNY Baruch - CS - 3157
C for Java Programmerslecture notes credits: Advanced Programming (cs3995, Spring 2002, Prof Schulzrinne) Software Construction (J. Shepherd) Operating Systems at Cornell (Indranil Gupta) today: Why learn C after Java? A brief background on C
CUNY Baruch - CS - 1007
CS1007 lecture #7 notestue 24 sep 2002 news homework #2 due tue oct 1 homework #1 should be returned in recitation this week short quiz #1 (5 points) in class on thu sep 26 bring one page of notes no computers, calculators, phones, etc. for, whil
CUNY Baruch - CS - 3157
lecture #16 mon oct 28, 2002news homework #3 due today homework #4 out today replacing quiz #3 see web page for updates. today programming tools overview conguration management sources: some slides from H. Schulzrinne, cs3995, spring 2002
CUNY Baruch - CS - 1007
CS1007 lecture #3 notestue 10 sep 2002 http:/www.columbia.edu/~cs1007 today: news quick and dirty UNIX quick and dirty emacs creating/editing/compiling/running your first Java program homework #1 submitting homework #1 cs1007-fall2002-s
CUNY Baruch - CS - 3157
Perl Regular Expressions: Kleene Star Kleene star and friends * operator ? match zero or more times Precedence is tighter than union ? /very* long string/Perl Regular Expressions: Special Characters Character class shortcuts \d = [0-9] \s = w
CUNY Baruch - CS - 3157
lecture #20 wed nov 13, 2002news homework #5 and #6 will be combined check class home page for updates today CGI HTML forms sources see references link on class web page cs3157-fall2002-sklar-lect201a.cgi#!/bin/sh echo &quot;Content-type:
CUNY Baruch - CS - 3157
read(2)read(2)NAMEread, pread, readv - read from fileSYNOPSIS#include &lt;sys/types.h&gt; #include &lt;sys/uio.h&gt; #include &lt;unistd.h&gt; ssize_t read(int fildes, void *buf , size_t nbyte); ssize_t pread(int fildes, void *buf , size_t nbyte, off_t offset)
CUNY Baruch - CS - 3157
exec(2)exec(2)NAMEexec, execl, execv, execle, execve, execlp, execvp - execute a fileSYNOPSIS#include &lt;unistd.h&gt; int execl(const char * path, const char *arg0, . . ., const char *argn, char * /*NULL*/); int execv(const char * path, char *cons
CUNY Baruch - CS - 1007
CS1007 lecture #22 notestue 26 nov 2002 news more recursion recursive searching reading: ch 12.7-12.12cs1007-fall2002-sklar-lect22 1recursion.recursion is defining something in terms of itself there are many examples in nature and in
CUNY Baruch - CS - 1007
CS1007 lecture #14 notesthu 24 oct 2002 news exams will be back on tuesday wrapper classes inheritance this keyword reading: ch 7 cs1007-spring2002-sklar-lect14 1classes.classes are the block around which Java is organized classes a
CUNY Baruch - CS - 1007
CS1007 lecture #5 notestue 17 sep 2002 news boolean expressions logical operators truth tables relational operators the if branching statement flowcharts command line arguments System.exit() method reading: ch 3.1-3.4news.homework #1 is
CUNY Baruch - CS - 1007
CS1007 lecture #7 notestue 24 sep 2002 news homework #2 due tue oct 1 homework #1 should be returned in recitation this week short quiz #1 (5 points) in class on thu sep 26 bring one page of notes no computers, calculators, phones, etc.
CUNY Baruch - CS - 1007
CS1007 lecture #12 notestue 15 oct 2002 news objects classes constants methods (review) encapsulation and visibility (the public and private modifiers) instantiation (the static modifier) reading: ch 6.1-6.7 objects.objects have: stat
CUNY Baruch - CS - 1007
CS1007 lecture #3 notestue 10 sep 2002 http:/www.columbia.edu/cs1007 today: news quick and dirty UNIX quick and dirty emacs creating/editing/compiling/running your rst Java program homework #1 submitting homework #1 news.Prof Sklar is
CUNY Baruch - CS - 1007
CS1007 lecture #20 notestue 19 nov 2002 news streams files java.io package exceptions StringTokenizer formatting output reading: ch 10-11 streams (1).we've drawn a picture of input and output many times this semester: input CPU output
CUNY Baruch - CS - 3157
Perl Regular Expressions Syntax for purpose of slides Regular expression = /pattern/ Broader syntax: if (/pattern/) {body} Notes Regular expressions are case sensitive /Rishikesh/ is not /rishikesh/ is not /RISHIKESH/ Regular expressions are