C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

Similar documents
C Characters and Strings

by Pearson Education, Inc. All Rights Reserved.

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Characters and Strings

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Computer Programming: Skills & Concepts (CP) Strings

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

C: How to Program. Week /May/28

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

C mini reference. 5 Binary numbers 12

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

CS 61c: Great Ideas in Computer Architecture

Array Initialization

Characters, Character Strings, and string-manipulation functions in C

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Chapter 8 - Characters and Strings

Chapter 8: Character & String. In this chapter, you ll learn about;

Chapter 8 C Characters and Strings

Module: Safe Programming. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CS61C : Machine Structures

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Pointers, Arrays, and Strings. CS449 Spring 2016

Lecture 05 Pointers ctd..

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

Create a Program in C (Last Class)

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

8. Characters, Strings and Files

DAY 3. CS3600, Northeastern University. Alan Mislove

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

CS 449 Lecture 3. Variables in memory. Dr. Jack Lange. int x = 4;

Intermediate Programming, Spring 2017*

LECTURE 15. String I/O and cstring library

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

CS107 Spring 2019, Lecture 4 C Strings

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Copyright (2004) Purdue Research Foundation. All rights reserved.

Principles of C and Memory Management

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Introduction to C/C++ Lecture 5 - String & its Manipulation

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

CS107, Lecture 4 C Strings

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

Scientific Programming in C V. Strings

Computer Programming

ECE 15B COMPUTER ORGANIZATION

Dynamic memory. EECS 211 Winter 2019

Personal SE. Functions, Arrays, Strings & Command Line Arguments

CSC Execution Environment. Program Execution Environment

Fundamentals of Programming & Procedural Programming

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Chapter 9 Strings. With this array declaration: char s[10];

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

CS 103 Lab - Party Like A Char Star

Outline. Computer Programming. Structure of a function. Functions. Function prototype. Structure of a function. Functions

Winter 2017 CS107 Midterm #2 Examination (Practice #2) Problem Points Score Grader TOTAL 30

C Programming Language Review and Dissection III

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Computer Systems C S Cynthia Lee

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

CS61C : Machine Structures

ECE551 Midterm Version 2

N v 1. Type generic string interfaces honor the const contract of application code ISO/IEC JTC 1/SC 22/WG14. August 20, 2016

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

QUIZ. What is wrong with this code that uses default arguments?

CS1100 Introduction to Programming

(9-3) Strings II H&K Chapter 8. Instructor - Andrew S. O Fallon CptS 121 (October 19, 2018) Washington State University

Object Oriented Programming COP3330 / CGS5409

Strings. Part III. 1) Introduction. 2) Definition. 3) Using Strings. 4) Input/Output. 5) Using string.h Library. 6) Strings as Function Parameters

CMPSC 311- Introduction to Systems Programming Module: Strings

HW1 due Monday by 9:30am Assignment online, submission details to come

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Strings. Steven R. Bagley

Memory, Arrays & Pointers

Coursework 2: Basic Programming

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

ONE DIMENSIONAL ARRAYS

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Binghamton University. CS-220 Spring Arrays in C

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Multidimension array, array of strings

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

More C Pointer Dangers

C++ for Java Programmers

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

MORE STRINGS AND RECURSION

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Introduction to N1031. Components of N1031. Guiding Principles. Walk through, issues, and rationale

CSC 2405: Computer Systems II

Transcription:

C strings (Reek, Ch. 9) 1

Review of strings Sequence of zero or more characters, terminated by NUL (literally, the integer value 0) NUL terminates a string, but isn t part of it important for strlen() length doesn t include the NUL Strings are accessed through pointers/array names string.h contains prototypes of many useful functions 2

String literals Evaluating dog results in memory allocated for three characters d, o, g, plus terminating NUL char *m = dog ; Note: If m is an array name, subtle difference: char m[10] = dog ; 10 bytes are allocated for this array This is not a string literal; It s an array initializer in disguise! Equivalent to { d, o, g, \0 } 3

String manipulation functions Read some source string(s), possibly write to some destination location char *strcpy(char *dst, char const *src); char *strcat (char *dst, char const *src); Programmer s responsibility to ensure that: destination region large enough to hold result source, destination regions don t overlap undefined behavior in this case according to C spec, anything could happen! char m[10] = dog ; strcpy(m+1, m); Assuming that the implementation of strcpy starts copying left-to-right without checking for the presence of a terminating NUL first, what will happen? 4

strlen() and size_t size_t strlen(char const *string); /* returns length of string */ size_t is an unsigned integer type, used to define sizes of strings and (other) memory blocks Reasonable to think of size as unsigned... But beware! Expressions involving strlen() may be unsigned (perhaps unexpectedly) if (strlen(x) strlen(y) >= 0)... avoid by casting: always true! ((int) (strlen(x) strlen(y)) >= 0) Problem: what if x or y is a very large string? a better alternative: (strlen(x) >= strlen(y)) 5

strcmp() string comparison int strcmp(char const *s1, char const *s2); returns a value less than zero if s1 precedes s2 in lexicographical order; returns zero if s1 and s2 are equal; returns a value greater than zero if s1 follows s2. Source of a common mistake: seems reasonable to assume that strcmp returns true (nonzero) if s1 and s2 are equal; false (zero) otherwise In fact, exactly the opposite is the case! 6

Restricted vs. unrestricted string functions Restricted versions: require an extra integer argument that bounds the operation char *strncpy(char *dst, char const *src, size_t len); char *strncat(char *dst, char const *src, size_t len); int strncmp(char const *s1, char const *s2, size_t len); safer in that they avoid problems with missing NUL terminators safety concern with strncpy: If bound isn t large enough, terminating NUL won t be written Safe alternative: strncpy(buffer, name, BSIZE); buffer[bsize-1] = \0 ; 7

String searching char *strpbrk(char const *str, char const *group); /* return a pointer to the first character in str that matches *any* character in group; return NULL if there is no match */ size_t *strspn(char const *str, char const *group); /* return number of characters at beginning of str that match *any* character in group */ 8

strtok string tokenizer char *strtok(char *s, char const *delim); /* delim contains all possible tokens : characters that separate tokens. if delim non-null: return ptr to beginning of first token in s, and terminate token with NUL. if delim is NULL: use remainder of untokenized string from the last call to strtok */ 9

strtok in action for ( token = strtok(line, whitespace); token!= NULL; token = strtok(null, whitespace)) printf( Next token is %s\n, token); d o g NUL c a t NUL NUL line NUL token 10

An implementation of strtok char* strtok(char *s, const char *delim) { static char *old = NULL; old contains the remains of an earlier s value char *token; (note use of static) if (! s) { s = old; if (! s) return NULL; } } if (s) { } s += strspn(s, delim); if (*s == 0) { old = NULL; return NULL; } token = s; s = strpbrk(s, delim); if (s == NULL) old = NULL; NULL has been passed in for s, so consult old strspn returns number of delimiters at beginning of s skip past these characters else { *s = 0; old = s + 1; } return token; strpbrk gives the position of the next delimiter. s is updated to this position, but token still points to the token to return. 11

Memory operations Like string operations, work on sequences of bytes but do not terminate when NUL encountered void *memcpy(void *dst, void const *src, size_t length); void *memcmp(void const *a, void const *b, size_t length); Note: memmove works like memcpy, but allows overlapping source, destination regions Remember, these operations work on bytes If you want to copy N items of type T, get the length right: memcpy(to, from, N * sizeof(t)) 12