COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

13
COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises

Transcript of COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Page 1: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

COMPSCI 210

Semester 1 - 2015

Tutorial 7 – C Exercises

Page 2: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Downloading and unpacking examples.tar.gz from Cecil

• Download examples.tar.gz from Cecil into your hard drive

• Go to: http://afsweb.ec.auckland.ac.nz/

Page 3: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Downloading and unpacking examples.tar.gz from Cecil cont.

– You will be directed to

– Select unixhome– You will be directed to

– Select Upload File(s)• Navigate to where you downloaded examples.tar.gz and upload it

Page 4: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Using University UNIX Account

• Connect from MS Windows using Putty

• All programs -> Putty• Under-grad server is

login.cs.auckland.ac.nz • Login using your UPI and

password• You can access the

machine from home if you have Putty installed on your home machine

From: S1-Basic C programming lecture’s slides

Page 5: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Login using your UPI and password

Page 6: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Login using your UPI and password cont.

Page 7: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Unpacking examples.tar.gz

• tar xvf examples.tar.gz

Page 8: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Compile and Run the countdown Program• Compile

gcc -o countdown countdown.c• Run

$ ./countdownEnter a positive number: 66543210

name of source code

name of executatble

From: S1-Basic C programming lecture’s slides

Page 9: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Compile and Run the countdown Program

Page 10: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Basic Linux Commands

• Creating a directory … mkdir dirname– Example: mkdir Assignment3

• Changing a directory … cd dirname– Example: cd Assignment3– Going back to the home directory: cd

• Listing the content of a directory… ls• Deleting a directory… rm -rf dirname

– Example: rm -rf Assignment3 … it deletes the folder and its contents

Page 11: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Exercise I

Write a C code to breakdown www.auckland.ac.nz web address into:

wwwaucklandacnz

Page 12: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

#include <string.h>Strtok()

char * strtok (char *restrict newstring, const char *restrict delimiters)• A string can be split into tokens by making a series of calls to the function strtok• The string to be split up is passed as the newstring argument on the first call only• Subsequent calls to get additional tokens from the same string are indicated by passing a null

pointer as the newstring argument• The delimiters argument is a string that specifies a set of delimiters that may surround the

token being extracted• On the next call to strtok, the searching begins at the next character beyond the one that

marked the end of the previous token• Note that the set of delimiters delimiters do not have to be the same on every call in a series

of calls to strtok• If the end of the string newstring is reached, or if the remainder of string consists only of

delimiter characters, strtok returns a null pointer.

Retrieved from: http://www.gnu.org/software/libc/manual/html_node/Finding-Tokens-in-a-String.html

Page 13: COMPSCI 210 Semester 1 - 2015 Tutorial 7 – C Exercises.

Solution #include <string.h>#include <stdio.h>int main(){ char address[40] = "www.auckland.ac.nz"; char delimiter[2] = "."; char *token; /* get the first part */ token = strtok(address, delimiter); /* get the rest */ while( token != NULL ) { printf( " %s\n", token ); token = strtok(NULL, delimiter); } return(0);}