208 New Book Review. String Type #include Assignment – string m=“Hi Mom”; – m=“Goodbye”; Concatenate - + – Can use variables or literals. Can use [ ]

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Lectures 10 & 11.
Strings.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 10.
Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Structure of a C program
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
C Slides and captured lecture (video and sound) are available at:
Chapter 9: Arrays and Strings
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 8 Arrays and Strings
Data Types.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
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.
C-Language Keywords(C99)
Chapter 8 Arrays and Strings
The Java Programming Language
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CPS120: Introduction to Computer Science
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Data Types Declarations Expressions Data storage C++ Basics.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
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 
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Python Let’s get started!.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
C Basics.
Primitive Types Vs. Reference Types, Strings, Enumerations
An overview of Java, Data types and variables
More About Data Types & Functions
Fundamental Programming
Problem 1 Given n, calculate 2n
Presentation transcript:

208 New Book Review

String Type #include Assignment – string m=“Hi Mom”; – m=“Goodbye”; Concatenate - + – Can use variables or literals. Can use [ ] to address a single character – char x=m[3]; – m[3]=‘r’;

More String Length or Size – int r=m.length(); – int r=m.size(); Find a substring – int r=m.find(“Hi”); – if (r == string::npos) cout<<“not found”<<endl; – r=m.find(“Hi”,pos) Starts looking at position pos in string m.

Even More String Stuff Substring – a part of a string – string p=m.substr(start, length); Swap – exchange two strings – m.swap(p);

C-Strings Do it yourself, no class to help. But there are some built-in functions. Array of characters. – char m[20], p[20]; – Can hold 19 characters; one for null/terminator (\0).

C-string Functions Comparison – strcmp – String CLASS can use, etc – C-strings use strcmp. Returns a value 0 for greater. Int i=strcmp(m,p); Assignment – strcpy – String CLASS can use = – strcpy(m,p); Length – int i=strlen(m); I/O - > work with both strings.

I/O stuff Get – Reads a single character. – cin.get(char c); – Everything is a character, blank, tab, newline – cin>>c; skips white space (blank, tab, newline) Ignore – ignore, enum, read left, right Peek- like get but does not move file pointer Putback – backs the file pointer one character

Nested Loops Discuss on board

Boolean Two values, true – false. int+int->int float*float->float Data type bool – Constants true, false – Cannot I/O them – int bool – bool && bool -> bool || ~ if (bool)….. while (bool)….

When to use for/while Can use interchangeably. But not good style. Use for when know how many times doing loop. Use while for unknown (waiting for a condition) times through the loop.

Assert #include assert (hours > 0); If condition is true, continues, if false, a message indicating problem is given and the program halts. Most useful for debugging.

Break – Continue Don’t need, can use other logic; I don’t like them, don’t use them Be aware of their existence.

Const Looks like a variable Cannot change value Good for self-documenting programs float PI=3.1415; Cannot change PI anywhere in the program Good for catching errors

Switch Useful to shorten many if – else if (i==1)…… else if (i==2) ….. else if (i==3) …. else ….. Switch switch (i) { case 1: …..;break; case 2: …..; break; case 3:……; break; default:……..}

?: - conditional Equivalent to if-else DON’T use. I don’t like it. Use ifs

Palindrome A string of characters that is the same forwards as backwards Ex. (ignoring blanks and quotes) – Madam I’m Adam – Can be applied to numbers when looking at digits as characters.

Value/Reference Parameters Arguments – what is passed by the invoking of the function Parameters – what “catches” the value passed into the function Value – a copy of the argument is made and that is passed. If the parameter is changed, the original argument is not changed Reference – the address of the argument variable is passed. If the parameter is changed, the original argument is changed

Value/Reference (cont.) Pass by value – default Pass by reference - & EXCEPT arrays – always pass by reference – do not use & Two reasons to pass by reference – Calling function expects/needs argument changed – Passing a LARGE variable (object, struct….). Pass by reference is more efficient If passing something large but don’t want to change pass as const &

Parallel Arrays Using two arrays where item k in one array is associated with item k in the other array. Ex. int id[100]; float gpa[100]; Could be done with an array of structs struct student { int id; float gpa; }; student everyone[100];

Globals One simple 95% rule: – DON’T USE THEM Needed in special systems programming Will see later Be happy with local variables and passing arguments.