Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Similar documents
Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

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.

Welcome! COMP s1. Programming Fundamentals

COMP s1 Lecture 1

COMP1917: 09 Arrays and Strings

Tutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White

printf( Please enter another number: ); scanf( %d, &num2);

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Computer Programming: Skills & Concepts (CP) Variables and ints

EECE.2160: ECE Application Programming Fall 2017

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

THE FUNDAMENTAL DATA TYPES

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Input/Output Week 5:Lesson 16.1

Fundamentals of Programming Session 8

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Program Verification! Goals of this Lecture! Words from the Wise! Testing!

16.216: ECE Application Programming Fall 2013

LESSON 4. The DATA TYPE char

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Data Types. Data Types. Integer Types. Signed Integers

EECE.2160: ECE Application Programming Spring 2018

Control Structure: Selection

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

Goals of this Lecture

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

War Industries Presents: An Introduction to Programming for Hackers Part III - Advanced Variables & Flow Control.

Fundamentals of Programming. Lecture 11: C Characters and Strings

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Repetition Structures II

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Fundamentals of Programming

Strings. Daily Puzzle

Fundamental of Programming (C)

COMP1511 revision lecture

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Create a Program in C (Last Class)

Computer Programming: Skills & Concepts (CP) Files in C

CS24 Week 2 Lecture 1

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

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

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Conditional Statement

Introduction: The Unix shell and C programming

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Fundamental of Programming (C)

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

8. Characters and Arrays

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Class Information ANNOUCEMENTS

Chapter 11 Introduction to Programming in C

Basic Types and Formatted I/O

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

C: How to Program. Week /Mar/05

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

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

1.00/1.001 Tutorial 1

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Arrays. Here is the generic syntax for an array declaration:

Chapter 11 Introduction to Programming in C

Chapter 13 Control Structures

Introduction To C Programming

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

EECE.2160: ECE Application Programming Fall 2016 Exam 1 Solution

Chapter 8 - Characters and Strings

More about Binary 9/6/2016

About this exam review

Imperative Programming

High Performance Computing MPI and C-Language Seminars 2009

H192 Midterm 1 Review. Tom Zajdel

Fundamental of Programming (C)

Algorithms, Data Structures, and Problem Solving

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

CS61C : Machine Structures

Transcription:

Welcome! COMP1511 18s1 Programming Fundamentals 0 COMP1511 18s1 Lecture 7 Strings Andrew Bennett 1 <andrew.bennett@unsw.edu.au> chars arrays of chars strings Before we begin 2 Overview after this lecture, you should be able to 3 understand the basics of chars introduce yourself to the person sitting next to you understand what ASCII is understand the basics of strings why did they decide to study computing? write programs using strings to solve simple problems (note: you shouldn t be able to do all of these immediately after watching this lecture. however, this lecture should (hopefully!) give you the foundations you need to develop these skills. remember programming is like learning any other language, it takes consistent and regular practice.)

Admin 4 Beyond Numbers 5 Don t panic! we ve mostly seen numbers thus far int age = 18; course style guide published double pi = 3.14 week 4 weekly test due friday what else might we want to store? don t be scared! assignment 1 out now work on it regularly! additional autotests added to the assignment don t forget about help sessions! see course website for details Letters and Words 6 Letters and Words 7 what about words? what about words? printf("andrew is awesome"); printf("andrew is awesome"); andrew is awesome

Letters and Words 8 introducing: strings 9 words in C are called strings printf("andrew is awesome"); andrew is awesome ^ this is a string Strings 10 Characters 11 a string is an array of characters. a character generally refers to a note: a character is a printed or written letter or symbol. letter, number, punctuation, etc. in C we call it a char

Characters in C 12 Characters in C 13 in C we call it a char char stores small integers. // making an int int age = 18; // making a char char letter = 'A'; 8 bits (almost always). mostly used to store ASCII character codes don t use for individual variables, only arrays only use char for characters (not to store e.g. numbers between 0-9) char s go inside single quotes, i.e. '. strings go inside double quotes, i.e. ". ASCII 14 ASCII 15 ASCII is a way of mapping numbers to characters. you don t have to memorize ASCII codes! it contains: single quotes give you the ASCII code for a character: 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. printf("%d", 'a'); // prints 97 printf("%d", 'A'); // prints 65 printf("%d", '0'); // prints 48 printf("%d", ' ' + '\n'); // prints 42 (32 + 10) don t put ASCII codes in your program - use single quotes instead!

ASCII 16 Reading chars 17 let s try it out! getchar() reads a byte from standard input returns an int returns a special value if it can t read a byte otherwise returns an integer (0..255) (ASCII code) let s try it out! getchar 18 getchar 19 consider the following code: int c1, c2; int c1, c2; printf("please enter first character:\n"); c1 = getchar(); printf("please enter second character:\n"); c2 = getchar(); printf("please enter first character:\n"); c1 = getchar(); printf("please enter second character:\n"); c2 = getchar(); printf("first %d\nsecond: %d\n", c1, c2); printf("first %d\nsecond: %d\n", c1, c2); what should this do? read two typed characters what should this do? what does it actually do? read one typed character + enter what does it actually do? (how can we fix it?)

getchar 20 End Of Input 21 how can we fix it? scanf or getchar will fail if there isn t any more input eg if you re reading from a file and reach the end of the file int c1, c2; printf("please enter first character:\n"); c1 = getchar(); getchar returns a special value to indicate no more input is available we call this value EOF getchar(); // extra getchar to catch the newline (how could you check this with scanf?) printf("please enter second character:\n"); c2 = getchar(); printf("first %d\nsecond: %d\n", c1, c2); Reading until End of Input 22 Strings 23 strings are an array of characters int c; c = getchar(); while (c!= EOF) { printf(" %c read, ASCII code is %d\n", c, c); c = getchar(); } reading numbers until end of input with scanf: int num; // scanf returns the number of items read while (scanf("%d", &num) == 1) { printf("you entered the number: %d\n", num); }

Remember Arrays? 24 Arrays in C 25 A series of boxes with a common type, all next to each other // Declare an array with 10 elements // and initialises all elements to 0. 0 1 2 3 4 5 6 7 8 9 10 11 int myarray[10] = {0}; 0 0 0 0 0 0 0 0 0 0 Arrays in C 26 Character Arrays 27 we can make an array of chars in the same way int myarray[10] = {0}; // Put some values into the array. myarray[0] = 3; myarray[5] = 17; char myarray[10] = {0}; // Put some values into the array. myarray[0] = 65; myarray[5] = 70; 3 0 0 0 0 17 0 0 0 0 65 0 0 0 0 70 0 0 0 0

How long is a piece of string? 28 How long is a piece of string? 29 you don t always know the length of a string in advance we need a way to know how long the string is! e.g. name could be Andrew, or Tom (6 characters vs 3 characters) name[0] = 'A'; name[1] = 'n'; name[2] = 'd'; name[3] = 'r'; name[4] = 'e'; name[5] = 'w'; // "Andrew" is 6 letters long name[0] = 'A'; name[1] = 'n'; name[2] = 'd'; name[3] = 'r'; name[4] = 'e'; A N D R E W name[5] = 'w'; (please never write code on one line like this! it s only here so the slides fit) // "Tom" is 3 letters long name[0] = 'T'; name[1] = 'o'; name[2] = 'm'; How long is a piece of string? 30 Null Terminator 31 we need a way to know how long the string is! we do this in C using a null terminator any function (e.g. printf) working with a string name[0] = 'T'; name[1] = 'o'; name[2] = 'm'; interprets this as end of string. 'T' 'O' 'M' R E W name[0] = 'T'; name[1] = 'o'; name[2] = 'm'; name[3] = '\0'; printing name would print TOMREW

Null Terminator 32 Sidenote: Uninitialised Arrays 33 what happens if we don t initialise our array? 'T' 'O' 'M' '\0' E W let s try it and see! printing name would now print TOM Sidenote: Uninitialised Arrays 34 Sidenote: Uninitialised Arrays 35 what s wrong with this code? the array has not been initialised int array[size]; int array[size]; int i = 0; while (i < SIZE) { printf("%d\n", array[i]); i++; } int i = 0; while (i < SIZE) { printf("%d\n", array[i]); i++; }??????????

Sidenote: Uninitialised Arrays 36 Sidenote: Uninitialised Arrays 37 solution: initialise the array first dcc can catch this for you if you tell it to use valgrind (note: you could also initialise all the values in a loop) dcc -o blah blah.c --valgrind int array[size] = {0}; int i = 0; while (i < SIZE) { printf("%d\n", array[i]); i++; } 0 0 0 0 0 0 0 0 0 0