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.

Slides:



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

1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 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 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
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.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Software and Software Vulnerabilities. Synopsis Array overflows Stack overflows String problems Pointer clobbering. Dynamic memory management Integer.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
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.
February 14, 2005 Characters, Strings and the String Class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
File IO and command line input CSE 2451 Rong Shi.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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.
Characters, Strings, And The string Class Chapter 10.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
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 Character Strings (Cstrings) Reference: CS215 textbook pages
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
Arrays, Vectors, and Strings Allocation and referencing.
Chapter 15 Strings as Character Arrays
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.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter Characters, Strings, and the string class 10.
Strings, Slide Fundamental Programming Strings.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
C Strings Doing strings the old fashioned way. strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
Strings (Continued) Chapter 13
FILE HANDLING IN C.
String What it is Why it’s useful
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Presentation transcript:

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 bytes –sizeof(‘x’)1 byte Every string in C++ is an array of characters with a NULL terminator to mark the end Every string in C++ is an array of characters with a NULL terminator to mark the end An array of characters w/o a NULL is not a string An array of characters w/o a NULL is not a string

char st3[5] will not compile: “initializer-string for array of chars is too long” - no room for \0

String I/O Functions All library functions assume your array of characters includes a NULL terminator. If not, result may be “segmentation fault, core dumped” iostream.h iostream.h coutprints all characters up to \0 cinreads a word (stops at a space) into a char array, appends a \0 cin.getlinereads a complete line of input into a char array, appends a \0 cin.getreads a single character, no \0

getline() Symtax getline, one of cin’s functions, uses default arguments for the length of the line to read, and the character to stop on. void cin.getline(char str[], int = 80, char = ‘\n’); dot cin’s getline function dot cin’s getline function cin.getline(name,80,’\n’);read at most 80 chars stop on newline cin.getline(name, 40, ‘\t’);read at most 40 chars stop on tab

Size matters If maximum length of first name is 20 char first[21]; If maximum length of last name is 30 char last[31]; If there will be at most two spaces between first and last names: char name[53];// \0 Invalid size character array declaration is a common cause of errors and system vulnerabilities

Reminder Strings are arrays of characters with a NULL marker or sentinel at the end Strings are arrays of characters with a NULL marker or sentinel at the end Array names are constant pointers to the element type Array names are constant pointers to the element type Other pointers can be used to access elements (offset or indices) Other pointers can be used to access elements (offset or indices) NULL is defined to be 0 (false) NULL is defined to be 0 (false)

Slide 3 of 32 // When loop finished, append NULL

Slide 4 of 32

Slide 5 of 32 This final version is very similar to the strcpy() included in the C++ string library

Visiting the Library C++ has an extensive library of string- handling functions C++ has an extensive library of string- handling functions Every C++ string function expects a pointer to an array of characters terminated by a NULL character Every C++ string function expects a pointer to an array of characters terminated by a NULL character Unterminated strings are a common cause of segmentation faults Unterminated strings are a common cause of segmentation faults Failure to check bounds is a common cause of system vulnerabilities Failure to check bounds is a common cause of system vulnerabilities

Slide 7 of 32

Slide 8 of 32

Slide 9 of 32

Slide 10 of 32

Slide 11 of 32

Slide 12 of 32

Slide 13 of 32

Slide 14 of 32

Slide 15 of 32

Slide 16 of 32

Slide 18 of 32 Note how much more memory is required by c4 – 80 bytes vs 16 bytes for c bytes for the strings stored “somewhere”

Slide 19 of 32

Slide 20 of 32

Errors char msg[5] = “Error”; // not enough space char line[80];// for 80 char line, need 81 in array Forgetting to put NULL into string Reading beyond end of array. Often happens because library routine expects to find a NULL Buffer overflow. Writing beyond end of array char *newloc; strcpy(newloc,“Don’t try this”); // need char* but must also have a block of memory set aside