1 Character Strings (Cstrings) Reference: CS215 textbook pages 442-449.

Slides:



Advertisements
Similar presentations
1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Advertisements

LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Chapter 10.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
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.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 9: Arrays and 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.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Computer Science 210 Computer Organization Strings in C.
1 Applied Arrays: Lists and Strings. 2 Chapter 13 Topics  Meaning of a List  Insertion and Deletion of List Elements  Selection Sort of List Elements.
Introduction to C programming
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 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 8 Arrays and Strings
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.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Fall 1999CS Composite Data Types. Fall 1999CS Topics to be Covered l Abstract data types l One-dimensional arrays l Two-dimensional arrays.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 15 Strings as Character Arrays
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
1 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Strings, Slide Fundamental Programming Strings.
1 Chapter 4 Program Input and the Software Design Process.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Chapter Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 13 Applied Arrays: Lists and Strings
String in C++ A string is an array of characters which contains a non-printing null character ‘\0’ ( with ASCII value 0 ) marking its end. A string.
Arrays and Strings Chapter 9.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
String What it is Why it’s useful
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 13 Applied Arrays: Lists and Strings
C++ Programming Lecture 20 Strings
Using string type variables
Presentation transcript:

1 Character Strings (Cstrings) Reference: CS215 textbook pages

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 be accessed by #include 2

3 What is a C String? A C string is a char array terminated by the null character ‘\0’ ( with ASCII value 0 ). A C string variable can be initialized in its declaration in two equivalent ways. char message [ 8 ] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’ }; char message [ 8 ] = “Hello” ; message [0] [1] [2] [3] [4] [5] [6] [7] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’

4 char vs. C string ‘A’ has data type char and is stored in 1 byte “A” is a C string of 2 characters and is stored in 2 bytes 5000 ‘A’ 6000 ‘A’ 6001 ‘\0’

5 Recall that... char message[8]; // declaration allocates memory To the compiler, the value of the identifier message alone is the base address of the array. We say message is a pointer (because its value is an address). It “points” to a memory location. message [0] [1] [2] [3] [4] [5] [6] [7] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ 6000

6 Aggregate C String I/O in C++ I/O of an entire C string is possible using the array identifier with no subscripts and no looping. EXAMPLE char message [ 8 ] ; cin >> message ; cout << message ; HOWEVER...

7 Extraction operator >> When using the extraction operator ( >> ) to read input characters into a string variable, l the >> operator skips any leading whitespace characters such as blanks and newlines l it then reads successive characters into the array, and stops at the first trailing whitespace character (which is not consumed, but remains waiting in the input stream) l the >> operator adds the null character to the end of the string

8 Example Using >> char name [ 5 ] ; cin >> name ; Suppose input stream looks like this: J o e name [0] name [1] name [2] name [3] name [4] 7000 total number of elements in the array null character is added ‘J’ ‘o’ ‘e’ ‘\0’

9 Function get( ) l because the extraction operator stops reading at the first trailing whitespace, >> cannot be used to input a string with blanks in it l if your string’s declared size is not large enough to hold the input characters and add the ‘\0’, the extraction operator stores characters into memory beyond the end of the array l use get function with 2 parameters to overcome these obstacles EXAMPLE char message [ 8 ] ; cin.get ( message, 8 ) ; // inputs at most 7 characters plus ‘\0’

10 inFileStream.get ( str, count + 1) l get does not skip leading whitespace characters such as blanks and newlines l get reads successive characters (including blanks) into the array, and stops when it either has read count characters, or it reaches the newline character ‘\n’, whichever comes first l get appends the null character to str l if it is reached, newline is not consumed by get, but remains waiting in the input stream

11 Function ignore( ) l can be used to consume any remaining characters up to and including the newline ‘\n’ left in the input stream by get EXAMPLE cin.get ( string1, 81 ) ; // inputs at most 80 characters cin.ignore ( 30, ‘\n’ ) ; // skips at most 30 characters // but stops if ‘\n’ is read cin.get ( string2, 81 ) ;

12 getline: removes newline l getline works the same as get EXCEPT: n It removes the newline character at the end of the line n You do not have to use ignore to remove it

13 Another Example Using get( ) char ch ; char fullName [ 31 ] ; char address [ 31 ] ; cout << “Enter your full name: “ ; cin.get ( fullName, 31 ) ; cin.get (ch) ; // to consume the newline cout << “Enter your address: “ ; cin.get ( address, 31 ) ; fullName [0] ‘N’ ‘e’ ‘l’ ‘l’ ‘ ’ ‘D’ ‘a’ ‘l’ ‘e’ ‘\0’... address [0] ‘A’ ‘u’ ‘s’ ‘t’ ‘i‘ ‘n’ ‘ ’ ‘T’ ‘X’ ‘\0’...

14 String Function Prototypes in int strlen (char str [ ] ); // FCTNVAL == integer length of string str ( not including ‘\0’ ) int strcmp ( char str1 [ ], char str2 [ ] ); // FCTNVAL== negative, if str1 precedes str2 lexicographically //== positive, if str1 follows str2 lexicographically //== 0, if str1 and str2 characters same through ‘\0’ char * strcpy ( char toStr [ ], char fromStr [ ] ); // FCTNVAL== base address of toStr ( usually ignored ) // POSTCONDITION : characters in string fromStr are copied to //string toStr, up to and including ‘\0’, //overwriting contents of string toStr 14

15 Converting Strings to Cstrings c_str member function of string class: string stringclass = “filename”; ifstream fin; fin.open(stringclass.c_str());

16 Cstrings to Strings Copy Cstrings to strings directly: char myCstring[4] = “abc”; string myString; myString = myCstring; // gets “abc”

17 String Class Arrays to Cstrings string myStringArray[100]; myStringArray[i] = “some characters”; To Cstring: //First index is number of elements //Second index is maximum length of each char myCharArray[100][21]; strcpy(myCharArray[i],myStringArray[i].c_str());

18 # include. char author [ 21 ] ; int length ; cin.get ( author, 21 ) ; length = strlen ( author ) ; // What is the value of length ? 5000 author [0] ‘C’ ‘h’ ‘i’ ‘p’ ‘ ’ ‘W’ ‘e’ ‘e’ ‘m’ ‘s’ ‘\0’....

19 char myName [ 21 ] = “Huang” ;// WHAT IS OUTPUT? char yourName [ 21 ] ; cout << “Enter your last name : “ ; cin.get ( yourName, 21 ) ; if ( strcmp ( myName, yourName ) == 0 ) cout << “We have the same name! “ ; else if ( strcmp ( myName, yourName ) < 0 ) cout << myName << “ comes before “ << yourName ; else if ( strcmp ( myName, yourName ) > 0 ) cout << yourName << “comes before “ << myName ; myName [0] ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’... yourName [0] ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’...

20 char myName [ 21 ] = “Huang” ; char yourName [ 21 ] ; if ( myName == yourName ) // compares addresses only! { // That is, 4000 and 6000 here.. // DOES NOT COMPARE CONTENTS!. } myName [0] ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’... yourName [0] ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’

21 char myName [ 21 ] = “Huang” ; char yourName [ 21 ] ; cin.get ( yourName, 21 ) ; yourName = myName; // DOES NOT COMPILE! // What is the value of myName ? myName [0] ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’... yourName [0] ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’...

22 char myName [ 21 ] = “Huang” ; char yourName [ 21 ] ; cin.get ( yourName, 21 ) ; strcpy ( yourName, myName ) ; // changes string yourName // OVERWRITES CONTENTS! myName [0] ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’... yourName [0] ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ ‘u’ ‘n’ ‘g’ ‘\0’