COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
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.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Chapter 10.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Arrays Chapter 6.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Introduction to C programming
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Introduction to Programming Lecture 8. String Handling  Character is the building block of strings.  Characters are represented inside the computer.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 15 Strings as Character Arrays
Sahar Mosleh California State University San MarcosPage 1 Character String.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Strings, Slide Fundamental Programming Strings.
ENEE150 – 0102 ANDREW GOFFIN Strings. Project 2 Flight Database 4 options:  Print flight  Print airport  Find non-stop flights  Find one-stop flights.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Suyash Bhardwaj Dept. of Computer Science & Engineering Faculty of Engineering & Technology Gurukul Kangri University, Haridwar l1l1 UNIT 1 String Processing.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Strings A string is a sequence of characters treated as a group
Arrays in C.
CS 1430: Programming in C++.
Week 9 – Lesson 1 Arrays – Character Strings
C++ Programming Lecture 20 Strings
CS31 Discussion 1H Fall18: week 6
EECE.2160 ECE Application Programming
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo

More on Array  CANNOT copy all elements of one array into another with an assignment statement array2 = array1;  Must use a loop instead for (int i=0; i<array_size; i++)  array2[i] = array1[i]; Lecture Review

Passing Arrays to Functions  Passing array as parameter = pass by reference  NO & sign To ensure no modification inside function  i.e. all elements in the array are read-only  Add a const modifier before the array parameter double func(const int array[])

Lecture Review Declare functions with array as parameter  1-D int func (int array[], int row); int func (int array[ ]);  2-D int func (int array[][ ], int row); int func (int array[ ][ ]);  E.g. int func1(int a[][]);// invalid! int func2(int a[][3]);// valid!

Lecture Review More E.g. void fun1(int a) {…} void main() { int lab_score[10][35]; fun1(lab_score[0][1]); } void fun1(int a[]) {…} void main() { int midterm[35]; fun1(midterm); } void fun1(int lab_score[][10]) {…} void main() { int lab_score[35][10]; fun1(lab_score); }

Lecture Review More on Char array  String A sequence of characters  Stored in an array of type char Ending  With the null character  ' \0 '

Lecture Review A character string is a sequence of characters enclosed in double quotes ( “ … ” ) E.g.  char s1[2] = “a”; s1[0] = ‘a’ s1[1] = ‘\0’  char b[] = “abcdef”;  char b[10] = “abcdef”;  char str[] = “”; It is called an empty string Uses one byte to store the null character (‘\0’)  char str[10]=“ ”; // invalid ‘a’‘a’‘b’‘b’‘c’‘c’‘d’‘d’‘e’‘e’‘f’‘f’‘ \0 ’‘a’‘a’‘b’‘b’‘c’‘c’‘d’‘d’‘e’‘e’‘f’‘f’ ???

Lecture Review cin stream operation  char s[10];  cin >> s; A string with space ( ‘ ’ ) CANNOT be read properly cin.getline  Syntax cin.getline(char dst[], int size, char delimiter)  When the delimiter character is encountered  A string is stored in character string dst  With maximum length of size, including the null character ( ‘ \0 ’ ) Note  delimiter character is optional, default is ‘ \n ’

Lecture Review What is the output of str in each case if the input is “COMP102 is i-chi-ban” ? char str[30]; cin.getline(str, 10); char str[30]; cin.getline(str, 10, ‘2’); char str[30]; cin.getline(str, 16, ‘-’); char str[30]; cin.getline(str, 25); char str[30]; cin.getline(str, 25, ‘i’); COMP102 i COMP10 COMP102 is i COMP102 COMP102 is i-chi-ban

Lecture Review String Copy  NEVER use ‘=’, except assign real val when declare char str[16] = “Hello World”; char str1[16] = str;  Copy source string into destination string void strcpy(char dest[], const char src[]); E.g.  strcpy(str1, str);

Lecture Review String Length  int strlen(const char[]); Returns length of string, NOT include ‘ \0 ’  E.g. 1. int str_length = strlen(“abcde”); 2. char str[10] = “abcde”; int str_length = strlen(str);

Lecture Review String Compare  NEVER use ( ==,, >= )  Compares strings string1 and string2 int strcmp(char string1[], char string2[]); E.g.  int result = strcmp(s1, s2);  if (s1 result = -1 (true)  if (s1 == s2) => result = 0 (false)  if (s1 > s2) => result = 1 (true)

Common Error The operator “ == ” doesn't test two strings for equality  E.g. if (string1 == string2)// Invalid! Use strcmp function instead  if (!strcmp(string1, string2))

SUMMARY By the end of this lab, you should be able to:  Pass array to function  Declare and manipulate char array as string

Lab11 Download the template from website