1 / 12

C++ Programming Lecture 20 Strings

C++ Programming Lecture 20 Strings. By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department. Outline. Introduction. String fundamentals. String manipulations. Examples. Fundamentals of Characters and Strings I. Character constant Integer value of a character

Download Presentation

C++ Programming Lecture 20 Strings

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. C++ ProgrammingLecture 20Strings By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

  2. Outline • Introduction. • String fundamentals. • String manipulations. • Examples. The Hashemite University

  3. Fundamentals of Characters and Strings I • Character constant • Integer value of a character • Single quotes • 'z' is the integer value of z, which is 122 • String • Series of characters treated as one unit • Can include letters, digits, special characters +, -, * ... • String literal (string constants) • Enclosed in double quotes, for example: "I like C++" • Array of characters, ends with null character '\0' • Strings are constant pointers (like arrays) • Value of string is the address of its first character The Hashemite University

  4. Fundamentals of Characters and Strings II • String assignment • Character array: char color[] = "blue"; • Creates 5 element char array, color, (last element is '\0'), the same effect of: char color[] = {‘b’, ‘l’, ‘u’, ‘e’, ‘\0’}; • variable of type char * char *colorPtr = "blue"; • Creates a pointer to string “blue”, colorPtr, and stores it somewhere in memory The Hashemite University

  5. Fundamentals of Characters and Strings III • Reading strings • Assign input to character array word[ 20 ] cin >> word • Reads characters until whitespace or EOF • String could exceed array size cin >> setw( 20 ) >> word; • Reads 19 characters (space reserved for '\0') • cin.getline • Reads a line of text • Using cin.getline cin.getline( array, size, delimiter character); The Hashemite University

  6. Fundamentals of Characters and Strings IV • cin.getline • Copies input into specified array until either • One less than the size is reached • The delimiter character is input • Example char sentence[ 80 ]; cin.getline( sentence, 80, '\n' ); • The third option is ‘\0’ by default. • cin.get() • Reads one character at a time entered from keyboard. • Use for loop to read the complete line or simply use cin.getline(). The Hashemite University

  7. String Manipulation Functions of the String-handling Library I • String handling library <cstring> provides functions to • Manipulate strings • Compare strings • Search strings • Tokenize strings (separate them into logical pieces) • ASCII character code • Strings are compared using their character codes • Easy to make comparisons (greater than, less than, equal to) • Tokenizing • Breaking strings into tokens, separated by user-specified characters • Tokens are usually logical units, such as words (separated by spaces) • "This is my string" has 4 word tokens (separated by spaces) The Hashemite University

  8. String Manipulation Functions of the String-handling Library II The Hashemite University

  9. String Manipulation Functions of the String-handling Library III The Hashemite University

  10. // Fig. 5.31: fig05_31.cpp // Using strtok. #include <iostream> using std::cout; using std::endl; #include <cstring> // prototype for strtok int main() { char sentence[] = "This is a sentence with 7 tokens"; char *tokenPtr; cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n"; // begin tokenization of sentence tokenPtr = strtok( sentence, " " ); // continue tokenizing sentence until tokenPtr becomes NULL while ( tokenPtr != NULL ) { cout << tokenPtr << '\n'; tokenPtr = strtok( NULL, " " ); // get next token } // end while cout << "\nAfter strtok, sentence = " << sentence << endl; return 0; // indicates successful termination } // end main Example The Hashemite University

  11. Example Output • Find it by yourself. The Hashemite University

  12. Additional Notes • This lecture covers the following material from the textbook: • Chapter 5: Section 5.12 The Hashemite University

More Related