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.

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

M ANIPULATING S TRINGS Resources [1] Object Oriented Programming with C++ (3 rd Edition) E Balagurusamy [2] Teach Yourself C++ (3 rd Edition) H Schildt.
 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 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
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.
File streams Chapter , ,
Chapter 10.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
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.
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
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
1 Principles of Computer Science I Honors Section Note Set 10 CSE 1341.
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.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 8 Strings and Vectors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
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
CSC 270 – Survey of Programming Languages
Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.
Chapter 15 Strings as Character Arrays
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
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.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 8 Strings and Vectors. Slide 8- 2 Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors.
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.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Lecture 2A Data Types Richard Gesick
Strings: C-strings vs. Strings as Objects
Characters, Strings, and the cstring Library
Characters, Strings, and the cstring Library
Standard Input/Output Streams
Standard Input/Output Streams
Object Oriented Programming COP3330 / CGS5409
C++ STRINGS string is part of the Standard C++ Library
Learning Objectives String Class.
Strings: C-strings vs. Strings as Objects
Engineering Problem Solving with C++, Etter
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

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

C-Strings Old version from the C language Arrays of Characters cout <”Hello”; |'H'|'e'|'l'|'l'|'o'|\0|...| Null character, \0, marks end of string

C-Strings Can initialize  Cannot assign  To Copy, use strcpy() function. In cstring library copyEr.cpp  Attempts an assignment copyCstring.c  Uses strcpy()

string class const string COLOR = “Green”; New type in C++  string is new type  C-string is old type string.cpp  Input reads to blank  Does allow assignments.

Conversion of string C-string = string constructor automatically converts C- string to string. string word; word = “fish”; char name[20]; name = word; //ERROR, NO //string to C-string // Conversion

Conversion of string C-string Some functions require C-string arguments. ifstream infile( filename);  Filename must be C-string .c_str() function does conversion writefile.cpp  Compiler error  Can fix with.c_str

getline string str; cin >> str; Reads up to white space. getline reads in whole line. sentence.cpp Syntax:  getline(inputStream, string,delimiter);  Default delimiter is '\n'  Delimiter not read into string

get Gets one character ifstream ins(“data.txt”); char c; ins.get(c);

More String Operations(p. 300)‏ Compare with ==, <=, etc Copy with = Concatenate with + string msg; msg = “Hello, “; msg = msg + “Class.\n” at msg.at(4) --> 'o' Some used in winner.cpp

Questions How many chars does it take to store “Kate” as a C-string? What does the following statement sequence print? string str = “Harry”; cout << str.at(0) << str.at(1); Give an input statement to read a name of the form “John Q. Public”.

Next Time Dynamic Arrays, Monday Read pp Then Friday review for the final.