1 / 110

Computer Science 1620

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 semester

skah
Télécharger la présentation

Computer Science 1620

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 1620 Strings

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

  3. 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;

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

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

  6. How is a C-string stored? • as an array of chars • index 0 holds first character • index 1 holds second character • … 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] = '!'; '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] How does C/C++ know how big the string is?

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

  8. 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; Array must be big enough to store all characters plus the terminating null character. 'H' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' '!' 0 x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9] x[10] x[11] x[12]

  9. Initializing character arrays • character arrays can be initialized in the same ways as other arrays • C++ allows you to initialize character arrays using a stringliteral • 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

  10. 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;

  11. 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;

  12. 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; }

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

  14. 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)

  15. 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; }

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

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

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

  19. 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; }

  20. 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; }

  21. 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; }

  22. Here, we are wasting 44 characters.

  23. 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?

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

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

  26. 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 }

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

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

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

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

  31. 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<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; }

  32. 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<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; }

  33. 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; }

  34. 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; }

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

  36. #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; }

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

  38. #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; }

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

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

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

More Related