CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Similar documents
Computer Programming: Skills & Concepts (CP) Strings

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

Create a Program in C (Last Class)

Computers Programming Course 11. Iulian Năstac

Pointers, Arrays, and Strings. CS449 Spring 2016

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

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

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

C Programming Language Review and Dissection III

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

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

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

Principles of C and Memory Management

Characters and Strings

8. Characters, Strings and Files

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Arrays and Strings. CS449 Fall 2017

Introduction to string

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

Procedural Programming & Fundamentals of Programming

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

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

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

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

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

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

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

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

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

DAY 3. CS3600, Northeastern University. Alan Mislove

Programming Language B

CMPSC 311- Introduction to Systems Programming Module: Strings

Array Initialization

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

Memory, Arrays & Pointers

Course organization. Course introduction ( Week 1)

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Lecture 05 Pointers ctd..

Intermediate Programming, Spring 2017*

Procedural Programming

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

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

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

Fundamentals of Programming & Procedural Programming

Character Strings. String-copy Example

LECTURE 15. String I/O and cstring library

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

by Pearson Education, Inc. All Rights Reserved.

Computers Programming Course 10. Iulian Năstac

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

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

ONE DIMENSIONAL ARRAYS

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

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

CSC209H1S Day Midterm Solutions Winter 2010

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

Princeton University Computer Science 217: Introduction to Programming Systems. Modules and Interfaces

Chapter 13. Strings. Introduction

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

BITG 1113: Array (Part 2) LECTURE 9

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CS 61c: Great Ideas in Computer Architecture

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

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C : Machine Structures

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

1 Pointer Concepts. 1.1 Pointer Examples

ECE551 Midterm Version 2

EM108 Software Development for Engineers

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

ECE551 Midterm Version 1

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

ECE 15B COMPUTER ORGANIZATION

Dynamic memory. EECS 211 Winter 2019

Pointers, Dynamic Data, and Reference Types

[6 marks] All parts of this question assume the following C statement. Parts (b) through (e) assume a variable called ptrs.

Computers Programming Course 12. Iulian Năstac

C Characters and Strings

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Chapter 8 - Characters and Strings

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 6

CSC231 C Tutorial Fall 2018 Introduction to C

Arrays, Strings, & Pointers

ARRAYS(II Unit Part II)

CS61C : Machine Structures

Kurt Schmidt. October 30, 2018

Arrays and Strings (2H) Young Won Lim 3/7/18

Object Oriented Programming COP3330 / CGS5409

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Scientific Programming in C V. Strings

Transcription:

CSC209H Lecture 4 Dan Zingaro January 28, 2015

Strings (King Ch 13) String literals are enclosed in double quotes A string literal of n characters is represented as a n+1-character char array C adds a \0 termination character after the string literal s characters String literals cannot be changed (they are read-only) printf ("Hello\n"); char *s = "Hello\n"; s[0] = q ; //Illegal

String Variables Any one-dimensional char array, with a null character at the end, is a string variable #define STR_LEN 80... char s[str_len + 1]; Initializing a string: char course[8] = "csc209h"; //Same as char course[8] = { c, s, c, 2, 0, 9, h, \0 };

String Length and Size Careful: there s an important difference between a string s length and size Length: number of characters before the null character Size: number of bytes allocated (e.g. one more than maximum length) //Size 10, length 3 char s[10] = "abc";

String Functions All of these are declared in string.h. String length: int strlen(const char *str); Copying a string: //Copy all of src (can be unsafe!) char *strcpy(char *dest, const char *src); //Copy at most n chars of src char *strncpy(char *dest, const char *src, int n);

strcpy and strncpy Both strcpy and strncpy return a pointer to dest This pointer is usually ignored, because it equals dest char s1[3]; char s2[5] = "abcd"; strcpy(s1, s2); //Overflow! strncpy(s1, s2, strlen(s2)); //Overflow! strncpy(s1, s2, sizeof(s1) - 1); //correct s1[sizeof(s1) - 1] = \0 ;

Concatenating Strings //Concatenate all of dest (can be unsafe!) char *strcat(char *dest, const char *src); //Add at most n chars from src to dest and add null char *strncat(char *dest, const char *src, int n); char s1[6] = "abc"; strncat (s1, "def", 6); //Overflow! //Correct strncat(s1, "def", sizeof(s1) - strlen(s1) - 1);

Comparing Strings //Return negative number, 0, or positive number //if s1 is <, =, or > s2, respectively int strcmp(const char *s1, const char *s2); There s also an strncmp, but strcmp isn t unsafe (why?) if (strcmp(s1, s2) <= 0) /*s1 <= s2*/

Searching for Characters //Search from left char *strchr(const char *s, int c); //Search from right char *strrchr(const char *s, int c); Both return a pointer to the character if found, NULL pointer if not found

Example: String Functions Problem: given a name string src of the format last first, return a string dest of the form first last. Steps: 1. Calculate the length of src (can use strlen) 2. Allocate space for dest (malloc) 3. Find the space in src (can use strchr) 4. Copy the first name (can use strcpy) 5. Add a space and a null terminator 6. Copy the last name (can use strncat)

What are Structures? (King 16.1-16.3) Data must be same type? Creation Access Array Yes [] syntax a[index] Structure No struct... s.name The following struct has three members. struct student { char first_name[20]; char last_name[20]; int year; };

Declaring Structure Variables Now, we can use struct student as a type, just like int or float Careful: it s struct student; using student by itself is incorrect (but read about typedef to fix this) To make some students and modify them... struct student student1, student2; strcpy(student1.first_name, "Dan"); strcpy(student1.last_name, "Z"); student1.year = 3;...

Structs and Functions Functions can take structs as parameters, and can return a struct back to the caller void printstudent (struct student s) { printf ("First name: %s\n", s.first_name); printf ("Last name: %s\n", s.last_name); printf ("Year: %d\n", s.year); }

Pointers to Structs... struct student student1; struct student *p; p = &student1; We have three ways to access or modify the members of student1 student1.year = 3; (*p).year = 3; p->year = 3; *p.year = 3; /*Wrong!*/

Pointers to Structs... For a function to be able to modify a struct s members, a pointer to the struct must be passed. void changefirst (struct student *s, char *new_first) { strcpy(s->first_name, new_first); }

Structs and Malloc (King Ch 17) malloc is often used in combination with structs (e.g. to create linked lists of structs) Dynamic memory allocation is required because we often don t know how many structs to allocate in advance struct student *s; //Uninitialized pointer! s = malloc (sizeof (student)); //Now s is fine strcpy(s->first_name, "Dan");