36
Prof. amr Goneid, AUC 1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 8. Characters & Strings

CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

  • Upload
    others

  • View
    20

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 1

CSCE 110PROGRAMMING FUNDAMENTALS

WITH C++

Prof. Amr GoneidAUC

Part 8. Characters & Strings

Page 2: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 2

Characters & Strings

Page 3: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 3

Characters & Strings Characters & their Operations The String Class: String Objects Declaration Input and Output Member Functions: Length & Indexing Copying , Concatenation & Comparison Other Member Functions Passing String Objects Arrays of Strings Conversions

Page 4: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 4

1. Characters & their Operations Characters: e.g. ‘A’ Character Constants & Variables:

e.g. const char qmark = ‘?’ ;char c;

I/O : e.g. cin >> c; cout << c; Comparison: e.g. (c >= ‘A’) && (c <= ‘Z’) Character Arrays: e.g. char a[20]; Type casting: e.g. int(‘A’) char(65)

Page 5: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 5

Some Character Manipulation Functions Conversion (to) Functions: toupper & tolower

e.g. char a , b ;b = toupper(a);

is Functions: return true or falseisalpha (A – Z , a – z)isdigit (0 – 9)isalnum (A – Z, a – z , 0 – 9)islower (a – z)isupper (A – Z)

Page 6: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 6

2. The String Class: String Objects String Class: Now part of standard library Use #include <string> Strings are “objects” of this class, Member functions

can be used Useful Link:www.cs.utexas.edu/~jbsartor/cs105/CS105_spr10_lec2.pptx.pdf

S t r i n g s a r e O b j e c t s

character

0 1 2

length-1

Page 7: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 7

3. Declaration

String Literals:“Hello”

Constant strings:const string greeting = “Hello ”;

String Objects:string firstName, lastName;string wholeName;string greeting = “Hello “;

Page 8: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 8

C-Style Strings C language has no predefined string data type. It uses character arrays to store strings but appends a

null character ‘\0’ at the end of the string.

e.g. char bird[ ] = “Eagle”; Some C-Style functions are used on C++ strings after

converting them to C-style

E a g l e \00 1 2

Page 9: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 9

Converting to C-Style Strings Example: to convert a character array containing digits

into its integer value. int atoi(const char s[])

Use the c_str member function to convert a string into a character array.

Example: string year = “2015"; int y = atoi(year.c_str());

Page 10: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 10

4. Input & Output

Use extraction operator >> and the stream cin for input ( stops at a blank)cin >> firstName;

Use insertion operator << and the stream cout for outputcout << greeting << wholeName <<

endl;

Page 11: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 11

Input & Output

getline (cin, wholeName, term);reads all characters typed in from the keyboard (including blanks) up to the character stored in term.Such character will not be added to the string.getline (cin, wholename);

will assume the default termination character (end of line or ‘\n’)

Page 12: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 12

5. Member Functions:Length & Indexing Dot notation used to call an objects member

functionswholeName.length();wholeName.at(i) Applies member function length and at to string

object wholeName 1st Function returns the objects length 2nd Function returns character at location [i] in

string object ( equivalent to wholeName [i] )

Page 13: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 13

6. Copying , Concatenation & Comparison Stores the first and last namewholeName = firstName + “ “ + lastName; Concatenation

+ joins the two objects together “ “ for string values not ‘ ‘ Compare two strings using == < > <= >= !=

if ( myName == wholeName ) …….

Page 14: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 14

Example

Build Inverse String:string aword , invword;cin >> aword;invword = “”; // Null Stringfor (i = aword.length()-1; i >= 0; i--)

invword += aword.at(i);cout << invword;

Page 15: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 15

StringOperations.cpp// FILE: StringOperations.cpp// ILLUSTRATES STRING OPERATIONS#include <iostream>#include <string>using namespace std;

int main (){

string firstName, lastName; string wholeName; string greeting = "Hello ";

cout << "Enter your first name: "; cin >> firstName;

Page 16: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 16

StringOperations.cpp

cout << "Enter your last name: "; cin >> lastName;

// Join names in whole name wholeName = firstName + " " + lastName;

// Display results cout << greeting << wholeName <<'!' << endl; cout << "You have " <<

(wholeName.length () - 1) <<" letters in your name." << endl;

Page 17: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 17

StringOperations.cpp

// Display initials cout << "Your initials are " <<

(firstName.at(0)) <<(lastName.at(0)) << endl;

return 0;}

Page 18: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 18

StringOperations.cpp

Program outputEnter your first name: CarynEnter your last name: Jackson

Hello Caryn Jackson!You have 12 letters in your name.Your initials are CJ

Page 19: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 19

7. Other Member Functions:

.find .append

.insert .swap

.replace .c_str

.erase

.assign

.substr

.empty

Page 20: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 20

.find

message.find(c) or message.find(sub)returns position of character (c) or start ofsubstring (sub) in message. If (c) or (sub) do not exist in message, function returns a long integer > message.length().e.g.message = “one two three”;message.find(‘e’) returns 2message.find(“two”) returns 4

Page 21: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 21

.insert

message.insert( s , newstring)returns message after inserting newstringstarting at location (s)e.g.message = “abcd”; s1 = “klm”; message.insert(2 , s1);changes message to be“abklmcd”

Page 22: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 22

.replace

message.replace(s , n , newstring)returns message after replacing (n)characters by newstring starting at location (s) e.g.message = “during last week”;message.replace(12 , 4 , “month”)changes message to “during last month”

Page 23: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 23

Example

Search and replace:p = message.find(sub);if ((p >= 0) && (p < message.length()))

message.replace(p,sub.length() , news);elsecout << sub << “ not found” << endl;

Page 24: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 24

.erase

message.erase(s , n)returns message after deleting n characters starting at location (s) e.g.message = “one two three”;message.erase(3 , 4)changes message to “one three”

Page 25: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 25

.assign

message.assign(olds , s , n)starting at location (s) in olds, assign tomessage the next n characters.e.g.olds = “abcdef”;message.assign(olds , 2 , 3)changes message to “cde”

Page 26: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 26

.substr

message.substr( s , n)return the substring consisting of (n) characters starting at location (s) inmessagee.g.message = “Good Morning”;cout << message.substr(0 , 2);outputs “Go”

Page 27: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 27

.empty()

message.empty()returns true if message is an emptystring, false otherwisee.g.if (message.empty()) ……….

Page 28: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 28

.append(str)

s.append(s1)returns s after appending s1 at its ende.g.if s = “Last”; s.append(“ Month”);changes s to be “Last Month”

Page 29: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 29

.swap(str)

s.swap(s1)swaps contents of s and s1e.g.if s = “First”; s1 = “Second”;s.swap(s1);changes s to be “Second” and s1 to be“First”

Page 30: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 30

.c_str() s.c_str( )

will return a constant character array having the contents of s and terminated by a null character ‘\0’, i.e. a C_style stringe.g.const char *cstr;cstr = cppstr.c_str();

Page 31: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 31

8. Passing String Objects

String objects are passed like ordinary variables, i.e., either by value or by reference.

If the string is input only, pass by value, e.g. void doit (string s)

If the string is Output or Inout, pass by reference, e.g. void doit (string& s)

Page 32: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 32

Examplevoid moneyToNumberString (string& moneyString){

// Local data . . .int posComma; // position of next comma

// Remove $ from moneyStringif (moneyString.at(0) == '$')

moneyString.erase(0, 1); else if (moneyString.find("-$") == 0)

moneyString.erase(1, 1);

Page 33: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 33

Example (cont)// Remove all commas

posComma = moneyString.find(",");while (posComma >= 0 && posComma <

moneyString.length()){

moneyString.erase(posComma, 1); posComma = moneyString.find(",");

}} // end moneyToNumberString

Page 34: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 34

9. Arrays of Strings

Strings can be elements of arrays.string names[100];declares an array of 100 strings.

names[i] refers to string [i] in the array. names[i].at(j) refers to character (j) of

string [i]. names[i] [j] the same as above

Page 35: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 35

10. Conversions

A c_string of digits can be converted into a number using one of the following functions:

Atoi(c_string) returns type intAtol(c_string) returns type longAtof(c_string) returns type double

Page 36: CSCI 110 STRUCTURED PROGRAMMING WITH C++rafea/CSCE110/Slides/8. Strings.pdf · Prof. amr Goneid, AUC 3 Characters & Strings Characters & their Operations The String Class: String

Prof. amr Goneid, AUC 36

Conversion Example#include <string>#include <iostream>#include <stdlib.h>using namespace std;

int main (){

string digits; const char *s; int num;cout << "Enter string of digits : "; cin >> digits;s = digits.c_str();num = atoi(s); cout << num << " " << 2*num << endl;return 0;

}