with c
play

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


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

  2. Characters & Strings Prof. amr Goneid, AUC 2

  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 Prof. amr Goneid, AUC 3

  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) Prof. amr Goneid, AUC 4

  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) Prof. amr Goneid, AUC 5

  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 0 1 2 S t r i n g s a r e O b j e c t s character length-1 Prof. amr Goneid, AUC 6

  7. 3. Declaration  String Literals: “Hello”  Constant strings: const string greeting = “Hello ”;  String Objects: string firstName, lastName; string wholeName; string greeting = “Hello “; Prof. amr Goneid, AUC 7

  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. 0 1 2 \0 E a g l e  e.g. char bird[ ] = “Eagle”;  Some C-Style functions are used on C++ strings after converting them to C-style Prof. amr Goneid, AUC 8

  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()); Prof. amr Goneid, AUC 9

  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; Prof. amr Goneid, AUC 10

  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’) Prof. amr Goneid, AUC 11

  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 object wholeName  1 st Function returns the objects length  2 nd Function returns character at location [i] in string object ( equivalent to wholeName [i] ) Prof. amr Goneid, AUC 12

  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 ) ……. Prof. amr Goneid, AUC 13

  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; Prof. amr Goneid, AUC 14

  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; Prof. amr Goneid, AUC 15

  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; Prof. amr Goneid, AUC 16

  17. StringOperations.cpp // Display initials cout << "Your initials are " << (firstName.at(0)) << (lastName.at(0)) << endl; return 0; } Prof. amr Goneid, AUC 17

  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 Prof. amr Goneid, AUC 18

  19. 7. Other Member Functions: .find .append .insert .swap .replace .c_str .erase .assign .substr .empty Prof. amr Goneid, AUC 19

  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 Prof. amr Goneid, AUC 20

  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” Prof. amr Goneid, AUC 21

  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” Prof. amr Goneid, AUC 22

  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; Prof. amr Goneid, AUC 23

  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” Prof. amr Goneid, AUC 24

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

  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); outputs “Go” Prof. amr Goneid, AUC 26

  27. .empty()  message.empty() returns true if message is an empty string, false otherwise e.g. if (message.empty()) ………. Prof. amr Goneid, AUC 27

  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” Prof. amr Goneid, AUC 28

  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” Prof. amr Goneid, AUC 29

  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(); Prof. amr Goneid, AUC 30

  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) Prof. amr Goneid, AUC 31

  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); Prof. amr Goneid, AUC 32

  33. Example (cont) // Remove all commas posComma = moneyString.find(","); while (posComma >= 0 && posComma < moneyString.length()) { moneyString.erase(posComma, 1); posComma = moneyString.find(","); } } // end moneyToNumberString Prof. amr Goneid, AUC 33

  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 Prof. amr Goneid, AUC 34

  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 Prof. amr Goneid, AUC 35

Recommend


More recommend