9-1 Learning Objectives  An Array Type for Strings  C-Strings.

Slides:



Advertisements
Similar presentations
Chapter 9 Strings. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives An Array Type for Strings C-Strings Character.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
 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.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chapter 10.
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.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
Chapter 9: Arrays and Strings
CS Nov 2006 C-strings.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
C-strings Array with base type char One character per indexed variable
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
ACS 168 Structured Programming Using the Computer Spring 2002 Joaquin Vila Prepared by Shirley White.
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.
Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string,
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
COP 3530 Data Structures & Algorithms Discussion Session 3.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 10 Strings and Multidimensional Arrays Slides by David B. Teague, Western Carolina.
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.
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 
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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students C-strings and strings.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
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.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C/C++ Basics (VI) Strings and Vectors Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem.
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.
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
Strings, Slide Fundamental Programming Strings.
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.
Today’s Material Strings Definition Representation Initialization
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
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
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 may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
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.
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.
Suyash Bhardwaj Dept. of Computer Science & Engineering Faculty of Engineering & Technology Gurukul Kangri University, Haridwar l1l1 UNIT 1 String Processing.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
Object Oriented Programming COP3330 / CGS5409
Learning Objectives String Class.
C-Strings, Strings, Vectors, STL
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CPS120: Introduction to Computer Science
Presentation transcript:

9-1 Learning Objectives  An Array Type for Strings  C-Strings

9-2 C-Strings  Array with base type char  One character per indexed variable  One extra character: "\0"  Called "null character"  End marker

9-3 C-String Variable  Array of characters: char s[10];  Declares a c-string variable to hold up to 9 characters  + one null character  Typically "partially-filled" array  Declare large enough to hold max-size string  Indicate end with null  Only difference from standard array:  Must contain null character

9-4 C-String Storage  A standard array: char s[10];  If s contains string "Hi Mom!", stored as:

9-5 C-String Initialization  Can initialize c-string: char myMessage[20] = "Hi there.";  Needn’t fill entire array  Initialization places "\0" at end  Can omit array-size: char shortString[] = "abc";  Automatically makes size one more than length of quoted string  NOT same as: char shortString[] = {‘a’, ‘b’, ‘c’};

9-6 C-String Indexes  A c-string IS an array  Can access indexed variables of: char ourString[5] = "Hi";  ourString[0] is "H"  ourString[1] is "i"  ourString[2] is "\0"  ourString[3] is unknown  ourString[4] is unknown

9-7 C-String Index Manipulation  Can manipulate indexed variables char happyString[7] = "DoBeDo"; happyString[6] = ‘Z’;  Be careful  Here, "\0" (null) was overwritten by a ‘Z’  If null is overwritten, c-string no longer "acts" like c-string!  Unpredictable results!

9-8 Library  Manipulations  Require library  Typically included when using c-strings

9-9 = and == with C-strings  C-strings not like other variables  Cannot assign or compare: char aString[10]; aString = "Hello";// ILLEGAL!  Can ONLY use "=" at declaration of c-string!  Must use library function for assignment: strcpy(aString, "Hello");  Built-in function (in )  Sets value of aString equal to "Hello"

9-10 Comparing C-strings  Also cannot use operator == char aString[10] = "Hello"; char anotherString[10] = "Goodbye";  aString == anotherString; // NOT allowed!  Must use library function again: if (strcmp(aString, anotherString)) cout << "Strings NOT same."; else cout << "Strings are same.";

9-11 Pre-defined C-string Functions in  strlen()  strcmp()  Strcpy()  Strcat()

9-12 C-string Functions: strlen()  "String length"  Often useful to know string length: char myString[10] = "dobedo"; cout << strlen(myString);  Returns number of characters  Not including null  Result here: 6

9-13 C-string Functions: strcat()  strcat()  "String concatenate": char stringVar[20] = "The rain"; strcat(stringVar, "in Spain");  result: stringVar now contains "The rainin Spain"  Be careful! Incorporate spaces if needed!

9-14 C-string Arguments and Parameters  Recall: c-string is array  So c-string parameter is array parameter  C-strings passed to functions can be changed by receiving function!  Like all arrays, typical to send size as well  Function "could" also use "\0" to find end  So size not necessary if function won’t change c-string parameter

9-15 C-String Output  Can output with insertion operator, <<  As we’ve been doing already: cout << news << " Wow.\n";  Where news is a c-string variable

9-16 C-String Input  Can input with extraction operator, >>  Issues exist, however  Watch size of c-string  Must be large enough to hold entered string!  C++ gives no warnings of such issues!

9-17 C-String Line Input  Can receive entire line into c-string  Use getline(), a predefined member function: char a[80]; cout << "Enter input: "; cin.getline(a, 80); cout << a << "END OF OUTPUT\n";  Dialogue: Enter input: Do be do to you! Do be do to you!END OF INPUT

9-18 More getline()  Can explicitly tell length to receive: char shortString[5]; cout << "Enter input: "; cin.getline(shortString, 5); cout << shortString << "END OF OUTPUT\n";  Results: Enter input: dobedowap dobeEND OF OUTPUT  Forces FOUR characters only be read  Recall need for null character!