WITH C++ Prof. Amr Goneid AUC Part 8. Characters & Strings - - PowerPoint PPT Presentation

with c
SMART_READER_LITE
LIVE PREVIEW

WITH C++ Prof. Amr Goneid AUC Part 8. Characters & Strings - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1
  • Prof. amr Goneid, AUC

1

CSCE 110 PROGRAMMING FUNDAMENTALS

WITH C++

  • Prof. Amr Goneid

AUC Part 8. Characters & Strings

slide-2
SLIDE 2
  • Prof. amr Goneid, AUC

2

Characters & Strings

slide-3
SLIDE 3
  • 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

slide-4
SLIDE 4
  • 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)

slide-5
SLIDE 5
  • 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 false

isalpha (A – Z , a – z) isdigit (0 – 9) isalnum (A – Z, a – z , 0 – 9) islower (a – z) isupper (A – Z)

slide-6
SLIDE 6
  • 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

slide-7
SLIDE 7
  • 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 “;

slide-8
SLIDE 8
  • 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

\0 0 1 2

slide-9
SLIDE 9
  • 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());

slide-10
SLIDE 10
  • 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 output cout << greeting << wholeName << endl;

slide-11
SLIDE 11
  • 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’)

slide-12
SLIDE 12
  • Prof. amr Goneid, AUC

12

  • 5. Member Functions:

Length & Indexing

 Dot notation used to call an objects member

functions wholeName.length(); wholeName.at(i)

 Applies member function length and at to string

  • bject wholeName

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

string object ( equivalent to wholeName [i] )

slide-13
SLIDE 13
  • Prof. amr Goneid, AUC

13

  • 6. Copying , Concatenation &

Comparison

 Stores the first and last name

wholeName = firstName + “ “ + lastName;

 Concatenation

+ joins the two objects together

 “ “ for string values not ‘ ‘  Compare two strings using == < > <= >= !=

if ( myName == wholeName ) …….

slide-14
SLIDE 14
  • Prof. amr Goneid, AUC

14

Example

 Build Inverse String:

string aword , invword; cin >> aword; invword = “”; // Null String for (i = aword.length()-1; i >= 0; i--) invword += aword.at(i); cout << invword;

slide-15
SLIDE 15
  • 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;

slide-16
SLIDE 16
  • 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;

slide-17
SLIDE 17
  • Prof. amr Goneid, AUC

17

StringOperations.cpp

// Display initials cout << "Your initials are " << (firstName.at(0)) << (lastName.at(0)) << endl; return 0; }

slide-18
SLIDE 18
  • Prof. amr Goneid, AUC

18

StringOperations.cpp

Program output

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

slide-19
SLIDE 19
  • Prof. amr Goneid, AUC

19

  • 7. Other Member Functions:

.find .append .insert .swap .replace .c_str .erase .assign .substr .empty

slide-20
SLIDE 20
  • Prof. amr Goneid, AUC

20

.find

 message.find(c) or message.find(sub)

returns position of character (c) or start of substring (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 2 message.find(“two”) returns 4

slide-21
SLIDE 21
  • Prof. amr Goneid, AUC

21

.insert

 message.insert( s , newstring)

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

slide-22
SLIDE 22
  • 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”

slide-23
SLIDE 23
  • 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); else cout << sub << “ not found” << endl;

slide-24
SLIDE 24
  • 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”

slide-25
SLIDE 25
  • Prof. amr Goneid, AUC

25

.assign

 message.assign(olds , s , n)

starting at location (s) in olds, assign to message the next n characters. e.g.

  • lds = “abcdef”;

message.assign(olds , 2 , 3) changes message to “cde”

slide-26
SLIDE 26
  • Prof. amr Goneid, AUC

26

.substr

 message.substr( s , n)

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

  • utputs “Go”
slide-27
SLIDE 27
  • Prof. amr Goneid, AUC

27

.empty()

 message.empty()

returns true if message is an empty string, false otherwise e.g. if (message.empty()) ……….

slide-28
SLIDE 28
  • Prof. amr Goneid, AUC

28

.append(str)

 s.append(s1)

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

slide-29
SLIDE 29
  • Prof. amr Goneid, AUC

29

.swap(str)

 s.swap(s1)

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

slide-30
SLIDE 30
  • 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 string e.g. const char *cstr; cstr = cppstr.c_str();

slide-31
SLIDE 31
  • 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)

slide-32
SLIDE 32
  • Prof. amr Goneid, AUC

32

Example

void moneyToNumberString (string& moneyString) { // Local data . . . int posComma; // position of next comma // Remove $ from moneyString if (moneyString.at(0) == '$') moneyString.erase(0, 1); else if (moneyString.find("-$") == 0) moneyString.erase(1, 1);

slide-33
SLIDE 33
  • 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

slide-34
SLIDE 34
  • 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

slide-35
SLIDE 35
  • 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 int

 Atol(c_string)

returns type long

 Atof(c_string)

returns type double

slide-36
SLIDE 36
  • 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; }