Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors email chat databases webpages etc.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

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.
Chapter 10.
Computer Science 1620 Loops.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Basic Elements of C++ Chapter 2.
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
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 
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
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.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
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. 1 Arrays and Vectors.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
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.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Strings.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
Arrays Arrays exist in almost every computer language.
Basic Elements of C++.
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Object Oriented Programming COP3330 / CGS5409
One-Dimensional Array Introduction Lesson xx
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Arrays Kingdom of Saudi Arabia
Wednesday 09/23/13.
String What it is Why it’s useful
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Arrays Arrays A few types Structures of related data items
Chapter 2 part #3 C++ Input / Output
Using string type variables
character manipulation
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Computer Science 1620 Strings

Programs are often called upon to store and manipulate text word processors chat databases webpages etc

In C++, we refer to sequence of characters as strings we've been using strings since the beginning of the semester cout << "Hello world!" << endl;

String storage in C++ two ways 1) A character array (C-strings) 2) The string type are these the same? similar, except the string type is much more flexible than the character array

C-Style Strings C Programmers did not have the string type to use a string, they had to use a character array it is useful to be able to use character arrays you may not always have the string type you will learn to appreciate the string type

How is a C-string stored? as an array of chars index 0 holds first character index 1 holds second character … 'H''e''l' 'o'' 'w''o''r''l''d''!' x[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]x[8]x[9]x[10]x[11] char x[12]; x[0] = 'H'; x[1] = 'e'; x[2] = 'l'; x[3] = 'l'; x[4] = 'o'; x[5] = ' '; x[6] = 'W'; x[7] = 'o'; x[8] = 'r'; x[9] = 'l'; x[10] = 'd'; x[11] = '!'; How does C/C++ know how big the string is?

Null-termination end-of-string symbolic constant shows program the end of the string corresponds to value 0 not character '0', this is value 48 numeric 0 has no corresponding character counterpart can be specified in a number of ways '\0' 0

char x[13]; x[0] = 'H'; x[1] = 'e'; x[2] = 'l'; x[3] = 'l'; x[4] = 'o'; x[5] = ' '; x[6] = 'W'; x[7] = 'o'; x[8] = 'r'; x[9] = 'l'; x[10] = 'd'; x[11] = '!'; x[12] = 0; 'H''e''l' 'o'' 'w''o''r''l''d''!' x[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]x[8]x[9]x[10]x[11] 0 x[12] Array must be big enough to store all characters plus the terminating null character.

Initializing character arrays character arrays can be initialized in the same ways as other arrays C++ allows you to initialize character arrays using a string literal Advantages automatically puts null at the end easier to type Must be big enough to store literal char x[13] = {'H','e','l','l','o',' ','W','o','r','l','d','!', '\0'}; char y[] = {'K', 'e', 'v', 'i', 'n', '\0'}; char x[13] = "Hello world!"; char y[] = "Kevin"; char x[10] = "Hello world!"; // error

String Output cout is equipped to handle null-terminated character array outputs each character until it finds the terminating null char x[13] = "Hello world!"; char y[] = "Kevin"; cout << x << endl; cout << y << endl;

String Input cin is equipped to handle null-terminated character array programmer must "guess" at a good size of array, to accommodate input powers of 2 are common char x[256]; cin >> x; cout << x << endl;

Line Input what is the output of the following program? assume I type "Kevin John Grant" at the prompt int main() { char name[256]; cout << "Please enter your full name: "; cin >> name; cout << "Name: " << name << endl; return 0; }

cin data tokenized by white space. How do we read in the entire line?

getline use the cin.getline function takes (up to) three parameters 1) the character array to read into 2) the maximum # of characters you wish to read typically, the size of the array 3) an optional delimiting (stopping) character if omitted, default is '\n' (newline)

Line Input what is the output of the following program? assume I type "Kevin John Grant" at the prompt int main() { char name[256]; cout << "Please enter your full name: "; cin.getline(name, 256, '\n'); cout << "Name: " << name << endl; return 0; }

Line Input previous program could also have been written: int main() { char name[256]; cout << "Please enter your full name: "; cin.getline(name, 256); cout << "Name: " << name << endl; return 0; } since no third parameter included, assumes '\n'

Advantages of Character Arrays easy to use setting/retrieving characters can be done using the array indexing syntax char str[256]; str[5] = 'K'; cout << str[6]; C++ has some conventions for making strings easier to handle placing string literals in char arrays handling char array input and output

Disadvantages of Character Arrays 1) Static Size the programmer must decide beforehand how much room to give a character array this leads to two problems: a) too much space b) too little space example: write a program that takes in a name of a person, and prints that name to the screen

Example Program how big do we make our character array? int main() { char name[???]; cout << "Please enter your name: "; cin.getline(name, ???); cout << "Your name is: " << name << endl; }

Example Program how big do we make our character array? how about 10? int main() { char name[10]; cout << "Please enter your name: "; cin.getline(name, 10); cout << "Your name is: " << name << endl; }

Example Program how big do we make our character array? to ensure correctness, better make it 50 int main() { char name[50]; cout << "Please enter your name: "; cin.getline(name, 50); cout << "Your name is: " << name << endl; }

Here, we are wasting 44 characters.

Disadvantages of Character Arrays 2) Difficult to modify suppose I have a string that contains "Saskatoon" I want to change that string to "Lethbridge" how do I do it?

Example Program how do I make the new string hold "Lethbridge"? int main() { char city[50] = "Saskatoon"; }

Example Program how do I make the new string hold "Lethbridge"? int main() { char city[50] = "Saskatoon"; city = "Lethbridge"; // will this work? }

Example Program we can only assign a string literal to a character array upon declaration int main() { char city[50] = "Saskatoon"; // this works city = "Lethbridge"; // this doesn't }

The String type a programmer-defined type not part of the actual C++ syntax rather, defined in a library (like cmath) must include to use this type

Advantages of String type over char array do not need to declare a size the string will "resize" itself as necessary you will learn how this is accomplished in future courses can reassign a string after it’s been declared just like an atomic variable

Example: #include using namespace std; int main() { string city = "Saskatoon"; cout << city << endl; city = "Lethbridge"; cout << city << endl; return 0; } String size not specified. Strings can be assigned new strings.

String Storage the string type stores its characters in an array however, it does not store the null- terminating character rather, it stores the size (# of characters) of the string however, these details are transparent to the user

String Operations 1) Output strings can be output using cout the value sent to output will be whatever value is stored in the string #include using namespace std; int main() { string city = "Saskatoon"; cout << city << endl; // outputs Saskatoon city = "Lethbridge"; cout << city << endl; // outputs Lethbridge return 0; }

String Operations 2) Input strings can be input using cin the value received by the string will be whatever the user types at the prompt #include using namespace std; int main() { string city; cout << "Where are you from: "; cin >> city; cout << "You are from " << city << endl; return 0; }

String Operations 2) Input the string type also allows line input use the getline function note: not cin.getline takes three parameters cin the string variable you are sending the value to an optional delimiting character ('\n' by default) #include using namespace std; int main() { string city; cout << "Where are you from: "; getline(cin, city, '\n'); cout << "You are from " << city << endl; return 0; }

String Operations 2) Input remember that if you want to read until a carriage return, the '\n' is optional #include using namespace std; int main() { string city; cout << "Where are you from: "; getline(cin, city); cout << "You are from " << city << endl; return 0; }

String Operations 3) Assignment a value can be assigned to a string using the assignment (=) operator very flexible in what you can assign to a string variable a string literal a null-terminated character array a single character another string

#include using namespace std; int main() { string str; str = "Hi"; cout << str << endl; char ca[] = "How are ya?"; str = ca; cout << str << endl; str = 'K'; cout << str << endl; string str2 = "Goodbye"; str = str2; cout << str << endl; return 0; }

String Operations 4) Comparison we can use a string with one of the relational operators (==, !=,, >=) compared letter by letter (as discussed previously) very flexible in what you can compare to a string variable a string literal a null-terminated character array another string variable

#include using namespace std; int main() { cout << boolalpha; string str = "Hi"; cout << (str == "Hi") << endl; char ca[] = "How are ya?"; cout ca) << endl; string str2 = "Hello"; cout << (str != str2) << endl; return 0; }

String Operations 5) Character Access with character arrays (C-style strings), we could get any character we wished using the subscript operator char x[] = "Kevin"; cout << x[2] << endl; // outputs v the string type gives us exactly the same feature string x = "Kevin"; cout << x[2] << endl; // outputs v x[0] = 'S'; x[3] = 'e'; cout << x << endl; // outputs Seven

String Operations 6) Size to obtain the size of a string, affix a.length() or.size() to the end of the variable name these types of function calls (class function calls) will be explained later on in this course string x = "Kevin"; cout << x.size() << endl; // outputs 5 x = "Lethbridge"; cout << x.length() << endl; // outputs 10

String Operations 7) Concatenation we can concatenate (fuse into one string) two strings using the + operator the + operator allows several types to be concatenated with a string variable a string literal a character array a single character another string

#include using namespace std; int main() { string str; str = "Hi"; cout << str + " there" << endl; char ca[] = ", how are ya?"; cout << (str + ca) << endl; cout << str + '!' << endl; string str2 = "dden"; cout << (str + str2) << endl; return 0; }

These 6 operations allow some fun stuff with strings 1: Write a program that reads a string from a user, and counts the number of 'e' characters that occur in that input

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { Step 1: Read a string from the user Step 2: Count the number of e's in that string Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { Step 1: Read a string from the user Step 2: Count the number of e's in that string Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); Step 2: Count the number of e's in that string Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); Step 2: Count the number of e's in that string Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); Step 2.1: Begin a count at 0 Step 2.2 For each 'e' in string, increment count Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); Step 2.1: Begin a count at 0 Step 2.2 For each 'e' in string, increment count Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; Step 2.2 For each 'e' in string, increment count Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; Step 2.2 For each 'e' in string, increment count Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; Step 2.2 For each character in string Step if the character is an 'e', inc. count Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; Step 2.2 For each character in string Step if the character is an 'e', inc. count Step 3: Output the number of e's in that string return 0; } Since strings allow array syntax, we can use our rules from arrays: 1) Begin an index at 0 2) Loop up to but not including size of string 3) Increment one at a time 4) Process character inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; Step if the character is an 'e', inc. count Step 3: Output the number of e's in that string return 0; } Since strings allow array syntax, we can use our rules from arrays: 1) Begin an index at 0 2) Loop up to but not including size of string 3) Increment one at a time 4) Process character inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); Step if the character is an 'e', inc. count Step 3: Output the number of e's in that string return 0; } Since strings allow array syntax, we can use our rules from arrays: 1) Begin an index at 0 2) Loop up to but not including size of string 3) Increment one at a time 4) Process character inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) Step if the character is an 'e', inc. count Step 3: Output the number of e's in that string return 0; } Since strings allow array syntax, we can use our rules from arrays: 1) Begin an index at 0 2) Loop up to but not including size of string 3) Increment one at a time 4) Process character inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) { Step if the character is an 'e', inc. count } Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) { Step if the character is an 'e' Step increment count } Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) { Step if the character is an 'e' Step increment count } Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) { if (input[i] == 'e') Step increment count } Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) { if (input[i] == 'e') Step increment count } Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) { if (input[i] == 'e') count++; } Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) { if (input[i] == 'e') count++; } Step 3: Output the number of e's in that string return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { string input; cout << "Please enter a string: "; getline(cin, input); int count = 0; for (int i = 0; i < input.size(); i++) { if (input[i] == 'e') count++; } cout << "Number of e's: " << count << endl; return 0; }

Example 2: Write a function that takes a string as its parameter, and returns the reverse of that string

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { Step 1: Create a new empty string Step 2: Place each character of input into the new string, in reverse order Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { Step 1: Create a new empty string Step 2: Place each character of input into the new string, in reverse order Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; Step 2: Place each character of input into the new string, in reverse order Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; Step 2: Place each character of input into the new string, in reverse order Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; Step 2: For each character of input (in reverse order) Step 2.1: Place each character into the new string Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; Step 2: For each character of input (in reverse order) Step 2.1: Place each character into the new string Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) Step 2.1: Place each character into the new string Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) Step 2.1: Place each character into the new string Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) result = result + input[i]; Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) result += input[i]; Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) result += input[i]; Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) result += input[i]; return result; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) result += input[i]; return result; } int main() { string input; cout << "Please enter a string: "; getline(cin, input); cout << "The reverse of that string is: " << reverse(input) << endl; return 0; }

Strings and const parameters since the string we are sending to reverse is not changed, we could send it as a reference parameter (so only one copy of the string exists in memory)

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(string &input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) result += input[i]; return result; } int main() { string input; cout << "Please enter a string: "; getline(cin, input); cout << "The reverse of that string is: " << reverse(input) << endl; return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string reverse(const string &input) { string result = ""; for (int i = input.size() – 1; i >= 0; i--) result += input[i]; return result; } int main() { string input; cout << "Please enter a string: "; getline(cin, input); cout << "The reverse of that string is: " << reverse(input) << endl; return 0; } By declaring parameter const, we guarantee that function will not change parameter.

Example 3: Write a function called change. Change takes a string s, and two characters, x and y. Change returns s with each occurrence of x changed to a y. Example: string str = "Hello World"; cout << change(str, 'l', 'r') << endl; // outputs Herro Worrd

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { Step 1: Create a new, empty string Step 2: Place each character of s into the new string. Step 2.1 If the character is an x, put a y instead Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { Step 1: Create a new, empty string Step 2: Place each character of s into the new string. Step 2.1 If the character is an x, put a y instead Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; Step 2: Place each character of s into the new string. Step 2.1 If the character is an x, put a y instead Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; Step 2: Place each character of s into the new string. Step 2.1 If the character is an x, put a y instead Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; Step 2: For each character in s Step 2.1 Place character of s into new string, unless character is an x, in which case, put y into the new string Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; Step 2: For each character in s Step 2.1 Place character of s into new string, unless character is an x, in which case, put y into the new string Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) Step 2.1 Place character of s into new string, unless character is an x, in which case, put y into the new string Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) Step 2.1 Place character of s into new string, unless character is an x, in which case, put y into the new string Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) { Step 2.1 If character is an x Step Place y in new string Step 2.2 Otherwise Step 2.2 Place character in string } Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == x) Step Place y in new string Step 2.2 Otherwise Step 2.2 Place character in string } Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == x) result += y; Step 2.2 Otherwise Step 2.2 Place character in string } Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == x) result += y; else Step 2.2 Place character in string } Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == x) result += y; else result += s[i]; } Step 3: Return the new string }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == x) result += y; else result += s[i]; } return result; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; string change(string s, char x, char y) { string result = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == x) result += y; else result += s[i]; } return result; } int main() { string str = "Hello World!"; cout << change(str, 'l', 'r') << endl; return 0; }

String type there are other functions associated with strings your textbook contains examples (find, substr,swap) ml ml