110
Computer Science 1620 Strings

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

  • View
    217

  • Download
    1

Embed Size (px)

Citation preview

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

Computer Science 1620

Strings

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

Programs are often called upon to store and manipulate textword processorsemailchatdatabaseswebpagesetc

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

In C++, we refer to sequence of characters as strings

we've been using strings since the beginning of the semestercout << "Hello world!" << endl;

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

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

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

C-Style StringsC 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 typeyou will learn to appreciate the string type

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

How is a C-string stored? as an array of chars

index 0 holds first character index 1 holds second character …

'H' 'e' 'l' '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?

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

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

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

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' '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.

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

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

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

String Outputcout 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;

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

String Inputcin is equipped to handle null-terminated

character arrayprogrammer must "guess" at a good size of

array, to accommodate inputpowers of 2 are common

char x[256];

cin >> x;

cout << x << endl;

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

Line Inputwhat 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;

}

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

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

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

getlineuse the cin.getline function takes (up to) three parameters

1) the character array to read into2) 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)

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

Line Inputwhat 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;

}

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

Line Inputprevious 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'

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

Advantages of Character Arrayseasy 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

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

Disadvantages of Character Arrays1) 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

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

Example Programhow 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;

}

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

Example Programhow 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;

}

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

Example Programhow 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;

}

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

Here, we are wasting 44 characters.

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

Disadvantages of Character Arrays2) Difficult to modify

suppose I have a string that contains "Saskatoon"

I want to change that string to "Lethbridge"how do I do it?

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

Example Programhow do I make the new string hold

"Lethbridge"?

int main() {

char city[50] = "Saskatoon";

}

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

Example Programhow do I make the new string hold

"Lethbridge"?

int main() {

char city[50] = "Saskatoon";

city = "Lethbridge"; // will this work?

}

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

Example Programwe 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

}

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

The String typea programmer-defined typenot part of the actual C++ syntax

rather, defined in a library (like cmath)must include <string> to use this type

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

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

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

Example:

#include<iostream>

#include<string>

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.

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

String Storage the string type stores its characters in an

arrayhowever, it does not store the null-

terminating character rather, it stores the size (# of characters) of

the stringhowever, these details are transparent to

the user

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

String Operations1) Output

strings can be output using cout the value sent to output will be whatever value is

stored in the string

#include<iostream>

#include<string>

using namespace std;

int main() {

string city = "Saskatoon";

cout << city << endl; // outputs Saskatoon

city = "Lethbridge";

cout << city << endl; // outputs Lethbridge

return 0;

}

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

String Operations2) Input

strings can be input using cin the value received by the string will be whatever

the user types at the prompt

#include<iostream>

#include<string>

using namespace std;

int main() {

string city;

cout << "Where are you from: ";

cin >> city;

cout << "You are from " << city << endl;

return 0;

}

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

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<iostream>

#include<string>

using namespace std;

int main() {

string city;

cout << "Where are you from: ";

getline(cin, city, '\n');

cout << "You are from " << city << endl;

return 0;

}

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

String Operations 2) Input

remember that if you want to read until a carriage return, the '\n' is optional

#include<iostream>

#include<string>

using namespace std;

int main() {

string city;

cout << "Where are you from: ";

getline(cin, city);

cout << "You are from " << city << endl;

return 0;

}

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

String Operations3) 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

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

#include<iostream>

#include<string>

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;

}

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

String Operations4) 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

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

#include<iostream>

#include<string>

using namespace std;

int main() {

cout << boolalpha;

string str = "Hi";

cout << (str == "Hi") << endl;

char ca[] = "How are ya?";

cout << (str > ca) << endl;

string str2 = "Hello";

cout << (str != str2) << endl;

return 0;

}

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

String Operations5) Character Access

with character arrays (C-style strings), we could get any character we wished using the subscript operatorchar x[] = "Kevin";

cout << x[2] << endl; // outputs v

the string type gives us exactly the same featurestring x = "Kevin";

cout << x[2] << endl; // outputs v

x[0] = 'S';

x[3] = 'e';

cout << x << endl; // outputs Seven

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

String Operations6) 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

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

String Operations7) 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

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

#include<iostream>

#include<string>

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;

}

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

These 6 operations allow some fun stuff with strings1: Write a program that reads a string from

a user, and counts the number of 'e' characters that occur in that input

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 stringStep 2.2.1 if the character is an 'e', inc. count

Step 3: Output the number of e's in that string

return 0;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 stringStep 2.2.1 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 02) Loop up to but not including size of string3) Increment one at a time4) Process character inside loop

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> using namespace std;

int main() {

string input; cout << "Please enter a string: "; getline(cin, input);

int count = 0; for (int i = 0;

Step 2.2.1 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 02) Loop up to but not including size of string3) Increment one at a time4) Process character inside loop

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.2.1 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 02) Loop up to but not including size of string3) Increment one at a time4) Process character inside loop

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.2.1 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 02) Loop up to but not including size of string3) Increment one at a time4) Process character inside loop

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.2.1 if the character is an 'e', inc. count }

Step 3: Output the number of e's in that string

return 0;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.2.1 if the character is an 'e' Step 2.2.1.1. increment count }

Step 3: Output the number of e's in that string

return 0;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.2.1 if the character is an 'e' Step 2.2.1.1. increment count }

Step 3: Output the number of e's in that string

return 0;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.2.1.1. increment count }

Step 3: Output the number of e's in that string

return 0;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.2.1.1. increment count }

Step 3: Output the number of e's in that string

return 0;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

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

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> using namespace std;

string reverse(string input) {

string result = "";

for (int i = input.size() – 1; i >= 0; i--) result += input[i];

return result;

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;

}

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

Strings and const parameterssince 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)

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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.

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

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

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.1.1 Place y in new string Step 2.2 Otherwise Step 2.2 Place character in string }

Step 3: Return the new string

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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 2.1.1 Place y in new string Step 2.2 Otherwise Step 2.2 Place character in string }

Step 3: Return the new string

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;

}

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

1. Read in the grades 2. Calculate the average A

3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <string> 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;}

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

String type there are other functions associated with

stringsyour textbook contains examples (find,

substr,swap)http://www.cppreference.com/cppstring/index.ht

ml