COMP1917: 09 Arrays and Strings

Similar documents
COMP1917: 15 File IO

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

Strings. Steven R. Bagley

Computer Science & Engineering 150A Problem Solving Using Computers

Arrays, Strings, & Pointers

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

ONE DIMENSIONAL ARRAYS

8. Characters, Strings and Files

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

Welcome! COMP s1. Programming Fundamentals

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

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

211: Computer Architecture Summer 2016

Simple Output and Input. see chapter 7

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

C programming basics T3-1 -

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

C mini reference. 5 Binary numbers 12

Contents. A Review of C language. Visual C Visual C++ 6.0

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

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

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

COMP s1 Lecture 1

Introduction to Programming Block Tutorial C/C++

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

CSC 2400: Computer Systems. Arrays and Strings in C

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

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Pointers and scanf() Steven R. Bagley

C Pointers. 6th April 2017 Giulio Picierro

C-String Library Functions

Course organization. Course introduction ( Week 1)

CSC 2400: Computer Systems. Arrays and Strings in C

File IO and command line input CSE 2451

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

C++ for Java Programmers

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.

Input/Output Week 5:Lesson 16.1

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

Fundamentals of Programming & Procedural Programming

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

Coursework 2: Basic Programming

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

2. Numbers In, Numbers Out

Text Output and Input; Redirection

Why arrays? To group distinct variables of the same type under a single name.

Multiple Choice Questions ( 1 mark)

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

Exercise 1.1 Hello world

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan

Create a Program in C (Last Class)

Object-Oriented Programming

Introduction to string

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

2009 S2 COMP File Operations

Chapter 8 C Characters and Strings

Fundamentals of Programming

ARRAYS(II Unit Part II)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

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

Fundamental of Programming (C)

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

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

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

BİL200 TUTORIAL-EXERCISES Objective:

20 Dynamic allocation of memory: malloc and calloc

IPC144 - Introduction to Strings. array of characters is an array, each element of which has a character data type.

arrays and strings week 3 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 imperative programming review

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

C Characters and Strings

today cs3157-fall2002-sklar-lect05 1

File I/O. Last updated 10/30/18

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

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

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

EE 355 Lab 4 - Party Like A Char Star

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

High Performance Computing MPI and C-Language Seminars 2009

Standard C Library Functions

String Computation Program

CS 261 Fall Mike Lam, Professor. Structs and I/O

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

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

CSE 374 Programming Concepts & Tools

Object oriented programming C++

Fundamentals of C Programming

Chapter 8 - Characters and Strings

C PROGRAMMING. Characters and Strings File Processing Exercise

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

C for C++ Programmers

CS 103 Lab - Party Like A Char Star

Transcription:

COMP1917: 09 Arrays and Strings Sim Mautner s.mautner@unsw.edu.au August 15, 2016 Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 1 / 14

Arrays int sum(int n1, int n2); int sum(int n1, int n2, int n3); int sum(int n1, int n2, int n3, int n4); int sum(int n1, int n2, int n3, int n4, int n5); int sum(int n1, int n2, int n3, int n4, int n5, int n6); int sum(int n1, int n2, int n3, int n4, int n5, int n6, int n7 Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 2 / 14

Arrays // Declare an array with 10 elements // and initialises all elements to 0. int myarray[10] = {0}; myarray 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 3 / 14

Arrays // Declare an array with 10 elements // and initialises all elements to 0. int myarray[10] = {0}; // Put some values into the array. myarray[0] = 3; myarray 0 3 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 4 / 14

Arrays // Declare an array with 10 elements // and initialises all elements to 0. int myarray[10] = {0}; // Put some values into the array. myarray[0] = 3; myarray[5] = 17; myarray 0 3 1 0 2 0 3 0 4 0 5 17 6 0 7 0 8 0 9 0 Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 5 / 14

Arrays // Declare an array with 10 elements // and initialises all elements to 0. int myarray[10] = {0}; // Put some values into the array. myarray[0] = 3; myarray[5] = 17; myarray[10] = 42; // <-- Error myarray 0 3 1 0 2 0 3 0 4 0 5 17 6 0 7 0 8 0 9 0 Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 6 / 14

Array Representation An array s name is a pointer to the first element of the array. Can be referred to as int num[] or int *num. Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 7 / 14

Arrays Exercises Ex 1: Write an application which creates an array with 15 numbers, reads in values and then prints out the array. Ex 2: Improve exercise 1 to use functions to load and print the array. Ex 3: Write a function (and an application to use it) which takes in an array of integers, and an integer, and returns the number of times that number occurs in the given array. Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 8 / 14

Multidimensional Arrays Arrays can have 2, 3 or more dimensions. Used to store data for easier access. Eg game boards (chess, checkers), data that s easier accessed via more than one dimension. Tic-Tac-Toe demo Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 9 / 14

Strings Text Hard-coded in double quotes (the same way a char is hard-coded in single quotes) Stored in memory as an array of characters Ensure enough space when creating the variable Has a null character ( \0 ) at the end to indicate the end of the string %s is the corresponding placeholder scanf doesn t require an ampersand scanf("%s", mystring); Reading a string using scanf, results in everything up to the first whitespace being read in Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 10 / 14

String Exercises Ex 1: Write a function which takes in a string and returns the number of characters in the string. int getstringlength(char * string); Ex 2: Write a function which copies one string into a second array of characters. int copystring(char * destination, char * source); Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 11 / 14

String Libraries #include <string.h> man string - execute on the terminal to get a list of functions available man strlen - execute on the terminal to get details on how to use the strlen function strlen - gets the length of a string strcmp - compares two strings, returns 0 if they re the same, negative number if they re in alphabetical (ascii) order, positive number if they re out of order strcasecmp - same as strcmp except ignoring case Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 12 / 14

fgets - Read a Whole Line fgets reads in a whole line of text, up to and including a newline character. Parameters: 1 String - the string to read into. 2 Size - the maximum number of characters to read in. (Don t forget to leave room for the null character.) 3 File - Where to read the string from. To read from the keyboard, use the keyword stdin Always includes the \n character at the end of the string. To remove the newline character: string[strlen(string)-1] = \0 ; Ex 3: Write an application which uses fgets to read in a line of text. Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 13 / 14

Commandline Arguments argc - the number of arguments (or parameters) passed into the application argv - an array of strings, each string is an argument to the application Ex 4: Write an application which prints out the commandline arguments it receives. Sim Mautner (UNSW) COMP1917: 09 Arrays and Strings August 15, 2016 14 / 14