EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages 490-508 in Chapter 7, and pages 550-558 in Chapter 8.  Homework #9 and Lab #9 due next week.

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Strings in C++ 04/29/11. Quiz Arrays  Section 7.1 & 7.2  Array basics  Partially filled arrays  Passing individual elements Mon. 05/02/11.
Chapter 10.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
CS Nov 2006 C-strings.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
EGR 2261 Unit 10 Records (structs)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 8 Arrays and Strings
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
COP 3530 Data Structures & Algorithms Discussion Session 3.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 15 Strings as Character Arrays
Programming Fundamentals. Today’s Lecture Multidimensional Arrays Arrays as Class Member Data Arrays of Objects C-Strings.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Computer Programming BCT 1113
Object Oriented Programming COP3330 / CGS5409
C Stuff CS 2308.
Chapter 8: The string Type
Review for Final Exam.
Strings A collection of characters taken as a set:
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3: Input/Output
Introduction to C++ Programming
Engineering Problem Solving with C++, Etter
Review for Final Exam.
Objectives In this chapter, you will:
Presentation transcript:

EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.  Quiz next week.

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

string Type To use data type string, a program must include the header file string. A string is a sequence of 0 or more characters. – The first character is in position 0. – The second character is in position 1, etc. Strings in C++ are enclosed in "". Binary operator + performs the string concatenation operation. Array subscript operator [] allows access to an individual character in a string. 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Additional string Operations 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Example 7-18: swap Function 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Chapter 8 Arrays and Strings

C -Strings (Character Arrays) Character array: an array whose components are of type char. C -strings are null-terminated ( '\0' ) character arrays. Example: – 'A' is the character A. – "A" is the C -string A. – "A" represents two characters, 'A' and '\0'. 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition

C -Strings (Character Arrays) (cont’d.) Example: char name[16]; Since C -strings are null terminated and name has 16 components, the largest string it can store has 15 characters. If you store a string whose length is less than the array size, the last components are unused. 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition

C -Strings (Character Arrays) (cont’d.) Size of an array can be omitted if the array is initialized during declaration. Example: char name[] = "John"; – Declares an array of length 5 and stores the C- string "John" in it. Useful C-string manipulation functions – strcpy, strcmp, and strlen 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition

C-String Comparison C -strings are compared character by character using the collating sequence of the system. – Use the function strcmp If using the ASCII character set: – "Air" < "Boat" – "Air" < "An" – "Bill" < "Billy" – "Hello" < "hello" 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Reading and Writing C-Strings Most rules for arrays also apply to C -strings (which are character arrays). C++ does not allow aggregate operations, including assignment, comparison, input, or output on arrays in general. But C++ does allow aggregate input and output of C -strings. 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition

C-String Input Example: cin >> name; – Stores the next inputted C-string into name. To read C-strings with blanks, use get function: cin.get(str, m+1); – Stores the next m characters into str but the newline character is not stored in str. – If input string has fewer than m characters, reading stops at the newline character. 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition

C-String Output Example: cout << name; – Outputs the content of name on the screen. – << continues to write the contents of name until it finds the null character. – If name does not contain the null character, then strange output may occur. << continues to output data from memory adjacent to name until a '\0' is found. 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Specifying Input/Output Files at Execution Time User can specify the name of an input and/or output file at execution time: 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition

string Type and Input/Output Files Argument to the open function must be a null- terminated string (a C -string). – If using a string variable for the name of an I/O file, the value must first be converted to a C -string before calling open. – Use the c_str function to convert. Syntax: strVar.c_str() where strVar is a variable of type string. 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition