Lets Explore a little bit about Arrays to

Let’s Explore a little bit about Arrays to Understand Objects of String Class l l Before a third of this course was dedicated to working in MATLAB, the last third of the course was about working with: ä a vector class and objects of the vector class ä a matrix class and objects of the matrix class We imported these classes using #include We did not use the built in C++ code for single and multiple dimensional arrays because they have no boundary checking. ä It is very easy to write code that accidently grabs memory locations that are just outside the memory locations set aside for the array. ä When this happens there is no way of knowing how that memory location is being used. It could be used for the operating system of the computer. ä If the above is the case, the computer crashes. 2. 1 Now we will learn a little about arrays and save the rest for MATLAB, a software system where everything is a matrix.

Using Functions from string class Chapter 9. 3 pp. 527 to 543 (required reading) Chapter 9. 4 & 9. 5 pp 555 to 562 (optional reading) and Very Brief Introduction to Arrays Chapter 7. 1 & 7. 2 pp. 385 to 399 (required reading) Chapter 7. 3 to 7. 9 pp. 399 to 460 (optional enrichment reading) The optional readings are there in case you have an interest or need to explore deeper. We will be covering arrays in more detail when we work in Matlab. 2. 2

What is an array? l l l We can declare several consecutive memory locations of the same data type under one name. An array is a data structure that groups these related data items together. Arrays are used when the data items are of the same type. Lets think of an array of students in this class. ä Each student has an index or subscript so that individual students can be accessed. ä The first student has the index of [0] sometimes refered to as sub 0. ä If there are 24 students in the array, the last student in the array will have the index of [23] All arrays begin with the index of [0]. 2. 3

Dramatization of Array of Students 1. Assign an index to each student in the class starting with 0 and ending with an index that is the number of students in class – 1. Call out to students[random valid index] with a command for some action. 1. Example: stand up, wave arm, say something, etc. Students[index called] will perform the action 2. 4

Strings and String Indexing l char is a C++ type whose data are single characters ä char ch = ‘a’; l l A string is an array of chars. String class was created so we do not have to deal with groups of chars but with a string. l We can refer to an individual character of a string with its index : Note below: single quote ‘ ‘ denotes a character, and a double quote “ “ denotes a string. l What is the output of the following code. Try it and see if you are right string s = “mathematics”; cout << s[7]<<endl; cout << s[0] <<endl; cout << s[4] <<endl; cout << s[10] <<endl; 2. 5 cout << s[34] <<endl; //not OK! Why? What is the output?

Strings : member functions • Function prototype for length ä int length( ); // Strings have length – returns the # of characters in a string • Function prototype for substr ä string substr(int pos, int len); //A substring is part of a string, substrings can be extracted from a string using member function substr(…) // returns substring of len, starting at position pos. Start counting at 0 • Function prototype for find int find(string s); //String member function find looks for an occurrence of one string in another, returns position of start of first occurrence. Again, 1 st position (or index) of a char in a string starts at 0. ä For more useful and cool functions see Table 9. 4 on pp 534 to 535 of the textbook. Try some just for fun after we explore the above functions. 2. 6

string member functions • This is called a member function. It applies a function to an object with the dot operator. Strings have length ä The function length() returns the number of characters string s = "hello"; int len = s. length(); // value of len is 5 cout << s[len-1] // What is the output? s = “”; len = s. length(); • // value of len here? Member functions are applied to objects using dot notation ä Cannot use length() without being applied to an object. 2. 7

• Finding substrings • A substring is part of a string, substrings can be extracted from a string using member function substr(…) string s = "theater"; int len = s. length(); string t = s. substr(0, 3); t = s. substr(1, 4); s = s. substr(3, 3); • // // value of len is ? ? t is "the", s is ? ? t is now ? ? ? s is ? ? t is ? ? Function prototype for substr string substr(int pos, int len); // pre: 0 <= pos < s. length() // post: returns substring of len characters // beginning at position pos // ok if len too big, NOT ok if pos too big 2. 8

Examples: • String member function find looks for an occurrence of one string in another, returns position of start of first occurrence ä If no occurrence, then string: : npos is returned string s = "I am the eggman"; int k = s. find("I"); // k is 0 k = s. find("he"); // k is 6 k = s. find("egg"); // what is k? k = s. find("a"); // what is k? k = s. find("walrus"); // what is k? s = "tcnj. edu"; k = s. find(". "); // what is k? if (k != string: : npos) { s = s. substr(k+1, s. length()); // what is s? } cout << "nn" << s << "nn”; 2. 9

Concatenation ä The function length() returns the number of characters ä string s = “dog"; string t = “house"; string c; c = s + t s = s + t ä ä ä //c = “doghouse” //s = “doghouse” We say that ‘+’ is overloaded. Operator overloading means that the operator (+) has different behaviors (int, double, string). 2. 10

Some Exercises int n = 0; string s = “computer science”; string t = “put” string w; l w = s. substr(1, 3); l n = s. find(“put”); l n = s. find (“dog”); l w += t; l n = s. length(); l n += t. length(); 2. 11
![Evaluate the following C++ expressions string word = “lease”; cout << word[3] <<endl; cout Evaluate the following C++ expressions string word = “lease”; cout << word[3] <<endl; cout](http://slidetodoc.com/presentation_image_h2/b592af706796b703ba3201a70922382e/image-12.jpg)
Evaluate the following C++ expressions string word = “lease”; cout << word[3] <<endl; cout << “p” + word << endl; string word 2 = word. substr(1, word. length()) cout << word 2 + word[0] << endl; Before going on to your assignment, let’s discuss another way to get input from the user: getline(cin, name); 2. 12 See next 5 slides. Slides 13 to 17.

Programs that Respond to Input Two ways to get input from the user: 1. cin << s --The extractor operator << extracts data from the user until a white space is encountered. If user is asked to enter name, it will extract only users first name when both first and last name is entered with a space between. string name; cout << “Please enter your full name. ”; Output cin >> name; Please enter your full name. Jane Doe cout << name; Jane 2. 13

Programs that Respond to Input Two ways to get input from the user: 1. cin << s --The extractor operator << extracts data from the user until a white space is encountered. If user is asked to enter name, it will extract only users first name when both first and last name is entered with a space between. string name; cout << “Please enter your full name. ”; Output cin >> name; Please enter your full name. Jane Doe cout << name; Jane 2. getline(cin, s) -- extracts a whole line including white spaces and end-of-line character. If user is asked to enter name, both first and last name with a space between will be extracted. s is the variable and cin is the input stream variable. string name; cout << “Please enter your full name. ”; getline(cin , name); Output cout<< name; Please enter your full name. Jane Doe 2. 14

Programs that Respond to Input Remember: getline(cin, s) -- extracts a whole line including white spaces and end-of-line character. 1. getline is useful for holding a screen until return is pressed. String s; Cout<<“Press return to continue. “; getline(cin, s) /*Output screen is halted until user presses return which is the end-of-line character read by getline and allows the display of more output. */ 2. 15

Programs that Respond to Input Remember: getline(cin, s) -- extracts a whole line including white spaces and end-of-line character. 1. getline is useful for holding a screen until return is pressed. String s; Cout<<“Press return to continue. “; getline(cin, s) /*Output screen is halted until user presses return which is the end-of-line character read by getline and allows the display of more output. */ 2. getline is useful for collecting the end-of-line character when cin >> and getline()are used alternately in code. Remember >> does not extract “n” and getline() does. string word, phrase, s; // definitions cout<<“Enter a word. “; cin >> word; cout<<“Enter a phrase for the word. “; getline(cin, phrase) cout<< word<<“: “<<phrase<<endl; Only the word will be displayed in the output. Why? 2. 16

Programs that Respond to Input Remember: getline(cin, s) -- extracts a whole line including white spaces and end-of-line character. 1. getline is useful for holding a screen until return is pressed. String s; Cout<<“Press return to continue. “; getline(cin, s) /*Output screen is halted until user presses return which is the end-of-line character read by getline and allows the display of more output. */ 2. getline is useful for collecting the end-of-line character when cin >> and getline()are used alternately in code. Remember >> does not extract “n” and getline() does. Only the word will be displayed in the output. Why? string word, phrase, s; // definitions cout<<“Enter a word. “; cin >> word; cout<<“Enter a phrase for the word. “: This can be corrected with a dummy getline() getline(cin, s); 2. 17 before the main one. getline(cin, phrase) Getline(cin, s); cout<< word<<“: “<<phrase<<endl;

Lab Assignment: String Manipulations Load up your template and fill in your Id and specs. 1. Use getline(cin, name) to get your first and last name or your first, middle, and last name if your name has too few letters in it. Example for me is: string name; getline(cin, name); > serita scott 2. Label the first set of words for your output as: Contiguous Letters or Adjecent Letters. Use substr(int, int) for words formed by contiguous letters. Example for me is: cout << name. substr(3, 2)<< endl; cout << name. substr(8, 3)<< endl; What is output in my examples? 3. Label the second set of words for your output as Mixed Letters. Pull letters from different parts of the name and put them togehter to make words. Example for me is: int k = s. find(“sc”); cout << s. substr(k, 2)+ s[5] + s[4] << endl; cout << s. substr(8, 2) + s[2] + s[1]<< endl; What is output in my examples? SEE NEXT SLIDE->->-> 2. 18

Lab Assignment: String Manipulations To get credit for this program will need to use: indexing [] to get single letters concatenation (+)to join letters or strings find(letter) substr(int, int) length() See example of length() on slides 7, 8, 11, & 12. Duscuss slide 12 in detail how it relates to this assignment. Special notes: 1. Remember to write a writeline << for every cout << 2. When using conctenation with indexing, get one character by using a substr(~, ~) which forces a production of letters instead of their ASCII code numbers. Only do this once and usually for the first single letter in the group. 3. If you are having trouble using length() function, see pages mentioned above. 4. Run the program after each word to see if the code is producing the words you want as you go along. 2. 19 Extra Credit: There are 9 exercises on p. 543. You get an extra credit point for each one you do.
- Slides: 19