C++ Programming Lecture 20 Strings

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
5.1Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C Characters and Strings
INC 161 , CPE 100 Computer Programming
C Characters and Strings
Fundamentals of Characters and Strings
Chapter 5 - Pointers and Strings
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
Chapter 5 - Pointers and Strings
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
String in C++.
String What it is Why it’s useful
Chapter 5 - Pointers and Strings
Chapter 5 - Pointers and Strings
C++ Programming Lecture 17 Pointers – Part I
5.1 Introduction Pointers Powerful, but difficult to master
Arrays Arrays A few types Structures of related data items
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 18 Pointers – Part II
Chapter 5 - Pointers and Strings
Lecture 2 Arrays & Pointers September 7, 2004
Characters and Strings Functions
C Characters and Strings
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

C++ Programming Lecture 20 Strings By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

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

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

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

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

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

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

String Manipulation Functions of the String-handling Library II char *strcpy( char *s1, const char *s2 ); Copies the string s2 into the character array s1. The value of s1 is returned. char *strncpy( char *s1, const char *s2, size_t n ); Copies at most n characters of the string s2 into the character array s1. The value of s1 is returned. char *strcat( char *s1, const char *s2 ); Appends the string s2 to the string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. char *strncat( char *s1, const char *s2, size_t n ); Appends at most n characters of string s2 to string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. int strcmp( const char *s1, const char *s2 ); Compares the string s1 with the string s2. The function returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. The Hashemite University

String Manipulation Functions of the String-handling Library III int strncmp( const char *s1, const char *s2, size_t n ); Compares up to n characters of the string s1 with the string s2. The function returns zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. char *strtok( char *s1, const char *s2); A sequence of calls to strtok breaks string s1 into “tokens”—logical pieces such as words in a line of text—delimited by characters contained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current to­ken is returned by each call. If there are no more tokens when the function is called, NULL is returned. size_t strlen( const char *s ); Determines the length of string s. The number of characters preceding the terminating null character is returned. The Hashemite University

The Hashemite University Example // 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 The Hashemite University

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

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