WEEK 9 Class Activities Lecturer’s slides.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

WEEK 12 Class Activities Lecturer’s slides.
CS1010 Programming Methodology
WEEK 5 Class Activities Lecturer’s slides.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
CS1010 Programming Methodology
UNIT 12 UNIX I/O Redirection.
CS1010 Programming Methodology
1 Strings ( מחרוזות ). 2 Agenda Definition and initialization Termination Input / Output String library.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Basic Input/Output and Variables Ethan Cerami New York
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
CS1010: Programming Methodology
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
CS1101: Programming Methodology Aaron Tan.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
CS1101: Programming Methodology Aaron Tan.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
COMPSCI 210 Semester Tutorial 7 – C Exercises.
WEEK 4 Class Activities Lecturer’s slides.
CS1101: Programming Methodology Aaron Tan.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
CS1010: Programming Methodology
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
WEEK 6 Class Activities Lecturer’s slides.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
UNIT 16 Characters and Strings.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
UNIT 11 Random Numbers.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
WEEK 8 Class Activities Lecturer’s slides.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
CS1101: Programming Methodology
Chapter 1 Basic C Programming
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
CS1010: Programming Methodology
WEEK 10 Class Activities Lecturer’s slides.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Week 12 Class Activities.
CS1010 Programming Methodology
Lecture 8 String 1. Concept of strings String and pointers
CS1010 Programming Methodology
CSE 303 Lecture 14 Strings in C
String manipulation string.h library
EECE.2160 ECE Application Programming
Strings #include <stdio.h>
Presentation transcript:

WEEK 9 Class Activities Lecturer’s slides

Week 9: Characters and Strings CS1010 (AY2014/5 Semester 1)Week9 - 2© NUS  Characters  Demos #1-3 from Unit #16  Exercise #1: Summing Digit Characters  Strings  Demos #4-7 from Unit #16  Exercise #2: Fill Strings  Exercise #3: Arrow Program  String function strtok()  Hangman Game  Play  Version 1  Exercise #4: Hangman version 2

Week 9 Programs CS1010 (AY2014/5 Semester 1)Week8 - 3© NUS  Download the programs from this web page   The files are:  Week9_Fill.c  Week9_Hangman_v1.c  Week9_strtok.c  You may also copy the above files directly into your sunfire account using the following UNIX command, where xxx is the name of one of the above files: cp ~cs1010/public_html/lect/prog/2014/week9_for_students/xxx.

Characters CS1010 (AY2014/5 Semester 1)Week9 - 4© NUS  We will go over the demo programs in Unit #16  Demo #1: Using Characters  Demo #2: Character I/O  Demo #3: Character Functions  Exercise #1: Summing Digit Characters

Ex #1: Summing Digit Characters (1/4) CS1010 (AY2014/5 Semester 1)Week9 - 5© NUS  Write a program Week9_SumDigits.c to read characters on a line, and sum the digit characters, ignoring the non-digit ones and everything after the first white space.  Use the appropriate functions introduced in Demos #2 and #3.  Two sample runs: Enter input: Sum = 35 Enter input: ^71()-2%:46" 9W35j Sum = 20

Ex #1: Summing Digit Characters (2/4) CS1010 (AY2014/5 Semester 1)Week9 - 6© NUS  Refer to this web page:  What is the input function needed if we do not want to use scanf())?  What header file to include besides ? getchar()  What are the character functions needed? isdigit() isspace()

Ex #1: Summing Digit Characters (3/4) CS1010 (AY2014/5 Semester 1)Week9 - 7© NUS  (After letting students think for 5 minutes on the algorithm…) How do we obtain an integer value from a digit character (let ch be the character variable)?  i.e.: ‘0’  0. ‘1’  1, …, ‘9’  9 Hint: ASCII value  What is the ASCII value of character ‘0’?  What is the ASCII value of character ‘1’?  …  What is the ASCII value of character ‘9’? ch – 48ch – ‘0’ or

Ex #1: Summing Digit Characters (4/4) CS1010 (AY2014/5 Semester 1)Week9 - 8© NUS  Now, write the program  (show the following code after students have completed theirs) #include int main(void) { char ch; int sum = 0; printf("Enter input: "); while (!isspace(ch = getchar())) if (isdigit(ch)) sum += ch - '0'; printf("Sum = %d\n", sum); return 0; }

Strings CS1010 (AY2014/5 Semester 1)Week9 - 9© NUS  We will go over the demo programs in Unit #16  Demo #4: String I/O  Demo #5: Remove Vowels  Demo #6: Character Array without termiating ‘\0’  Demo #7: String Functions  Quick Quiz  Exercise #2: Fill Strings  Exercise #3: Arrow Program  Hangman Game  Hangman version 1  Exercise #4: Hangman version 2

Quick Quiz CS1010 (AY2014/5 Semester 1)Week9 - 10© NUS 1.Are 'A' and "A" the same thing? 2.Can you do this?  char ch = 'at'; 3.Can char be used in a switch statement? How about a string? No char – yes string – no No

Exercise #2: Fill Strings CS1010 (AY2014/5 Semester 1)Week9 - 11© NUS  Write a program Week9_Fill.c to fill a 40-character line with duplicate copies of a string entered by user.  The length of the string entered is between 1 and 10.  The incomplete program Week9_Fill.c is given.  Sample runs: Enter string (between 1 and 10 characters): abcdefg Line: abcdefgabcdefgabcdefgabcdefgabcdefg Enter string (between 1 and 10 characters): abcdefg Line: abcdefgabcdefgabcdefgabcdefgabcdefg Enter string (between 1 and 10 characters): AA Line: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Enter string (between 1 and 10 characters): AA Line: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Enter string (between 1 and 10 characters): 1x2 Line: 1x21x21x21x21x21x21x21x21x21x21x21x21x2 Enter string (between 1 and 10 characters): 1x2 Line: 1x21x21x21x21x21x21x21x21x21x21x21x21x2  Which string functions (besides strlen()) come to your mind? strcat() strcpy()

Exercise #3: Arrow Program (1/2) CS1010 (AY2014/5 Semester 1)Week9 - 12© NUS  Write a program Week9_Arrow.c to randomly select a student to answer question.  The program reads in a list of names and use rand() to randomly select one of the names.  When a name is selected, the program will print out the first name, followed by the last name. For example, if the selected name is Tan Mei Ling. The program will print: Mei Tan, would you please answer the question?  You may assume that each name contains at most 30 characters, and there are at most 12 names.

Exercise #3: Arrow Program (2/2) CS1010 (AY2014/5 Semester 1)Week9 - 13© NUS  A sample run: Enter names: Gary Tan Khoo Siau Cheng Hsu Wynne Lee Mong Li Aaron Tan Zhou Lifeng Zhao Jin (user pressed ctrl-d here) Name selected: Siau Khoo, would you please answer the question? Enter names: Gary Tan Khoo Siau Cheng Hsu Wynne Lee Mong Li Aaron Tan Zhou Lifeng Zhao Jin (user pressed ctrl-d here) Name selected: Siau Khoo, would you please answer the question?  Hint: Use strtok()

String function: strtok() (1/2) CS1010 (AY2014/5 Semester 1)Week9 - 14© NUS  To break a string into a series of tokens using some specified delimiter(s).  Read the following site:   The first time you call strtok() you pass it: (1) the string you want to tokenise, and (2) a delimiter string.  For subsequent calls, you pass it: (1) NULL as the first paramater to tokenise the same string, and (2) a delimiter string. char *strtok(char *str, const char *delim)

String function: strtok() (2/2) CS1010 (AY2014/5 Semester 1)Week9 - 15© NUS #include int main(void) { char str[80] = "This is website"; char s[2] = "-"; char *token; /* get the first token */ token = strtok(str, s); /* walk through other tokens */ while (token != NULL) { printf("%s\n", token); token = strtok(NULL, s); } return 0; } Week9_strtok.c Output: This is website

Hangman Game CS1010 (AY2014/5 Semester 1)Week9 - 16© NUS Let’s play!

Hangman Game version 1 (1/5) CS1010 (AY2014/5 Semester 1)Week9 - 17© NUS  Week9_Hangman_v1.c  Assume that a player is given 5 lives.  Each incorrect guess  reduce the number of lives.  Each correct guess  display the letter in the word.

Hangman Game version 1 (2/5) CS1010 (AY2014/5 Semester 1)Week9 - 18© NUS Sample run #1: Number of lives: 5 Guess a letter in the word _ _ _ _ _ h Number of lives: 4 Guess a letter in the word _ _ _ _ _ p Number of lives: 4 Guess a letter in the word _ p p _ _ b Number of lives: 3 Guess a letter in the word _ p p _ _ m Number of lives: 2 Guess a letter in the word _ p p _ _ x Number of lives: 1 Guess a letter in the word _ p p _ _ i Sorry, you’re hanged! The word is "apple". Sample run #2: Number of lives: 5 Guess a letter in the word _ _ _ _ _ p Number of lives: 5 Guess a letter in the word _ p p _ _ e Number of lives: 5 Guess a letter in the word _ p p _ e o Number of lives: 4 Guess a letter in the word _ p p _ e a Number of lives: 4 Guess a letter in the word a p p _ e l Congratulations! The word is "apple".

Hangman Game version 1 (3/5) CS1010 (AY2014/5 Semester 1)Week9 - 19© NUS #include int has_letter(char [], char); int main(void) { char input; char word[] = "apple"; char temp[] = "_____"; int i, count = 0; int num_lives = 5; int length = strlen(word); #include int has_letter(char [], char); int main(void) { char input; char word[] = "apple"; char temp[] = "_____"; int i, count = 0; int num_lives = 5; int length = strlen(word); Week9_Hangman_v1.c

Hangman Game version 1 (4/5) CS1010 (AY2014/5 Semester 1)Week9 - 20© NUS do { printf("Number of lives: %d\n", num_lives); printf("Guess a letter in the word "); puts(temp); scanf(" %c", &input); if (has_letter(word, input)) { for (i=0; i<length; i++) if ((input == word[i]) && (temp[i] == '_')) { temp[i] = input; count++; } else num_lives--; } while ((num_lives != 0) && (count != length)); if (num_lives == 0) printf("Sorry, you're hanged! The word is \"%s\"\n", word); else printf("Congratulations! The word is \"%s\"\n", word); return 0; } do { printf("Number of lives: %d\n", num_lives); printf("Guess a letter in the word "); puts(temp); scanf(" %c", &input); if (has_letter(word, input)) { for (i=0; i<length; i++) if ((input == word[i]) && (temp[i] == '_')) { temp[i] = input; count++; } else num_lives--; } while ((num_lives != 0) && (count != length)); if (num_lives == 0) printf("Sorry, you're hanged! The word is \"%s\"\n", word); else printf("Congratulations! The word is \"%s\"\n", word); return 0; } Week9_Hangman_v1.c

Hangman Game version 1 (5/5) CS1010 (AY2014/5 Semester 1)Week9 - 21© NUS // Check whether word contains ch int has_letter(char word[], char ch) { int j; int length = strlen(word); for (j=0; j<length; j++) { if (ch == word[j]) return 1; } return 0; // ch does not occur in word } // Check whether word contains ch int has_letter(char word[], char ch) { int j; int length = strlen(word); for (j=0; j<length; j++) { if (ch == word[j]) return 1; } return 0; // ch does not occur in word } Week9_Hangman_v1.c Note: It is better to call strlen(word) just once and save the length in a variable, instead of calling strlen(word) multiple times as a condition in the ‘for’ loop.

Ex #4: Hangman Game version 2 CS1010 (AY2014/5 Semester 1)Week9 - 22© NUS  Modify the program Week9_Hangman_v1.c to Week9_Hangman_v2.c as follows: 1.Program will keep a list of 10 words and randomly choose a word from this list for the user to guess. (Each word is at most 15 characters long.) 2.Allow user the option to exit the game or guess the next word.

Things-To-Do CS1010 (AY2014/5 Semester 1)Week Deadline for Lab #4 18 October 2014, Saturday, 9am Continue to do practice exercises on CodeCrunch © NUS

End of File CS1010 (AY2014/5 Semester 1)Week9 - 24© NUS