CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Chapter 10.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
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 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CSC 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
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.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CS31: Introduction to Computer Science I Discussion 1A 5/14/2010 Sungwon Yang
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Chapter 15 Strings as Character Arrays
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
INC 161 , CPE 100 Computer Programming
Fundamentals of Characters and Strings
Pointers and Pointer-Based Strings
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CPS120: Introduction to Computer Science
C++ Programming Lecture 20 Strings
CS31 Discussion 1H Fall18: week 6
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang

Quick Review What did we learn last week? Arrays create the same type of multiple variables at one time index numbers start from 0 each item of an array is a regular variable array declaration specify the length of the array – the number must be a constant positive integer – value itself or const type of int variable initialization of an array values enclosed in curly braces number of values can be less than the length of an array if omit the length of an array, it is determined based on the number of values out-of-bound index c++ compiler does not check the bound of index – accessing out-of-bound items does not cause a compile-error – be careful not to access out-of-bound items in loop arrays in functions can pass individual items either by value or reference can pass the entire array – can be regarded as passing by reference const keyword – protects the items of the array from accidentally being changed

C-strings C++ is enhanced from C Language – The C Language is a proper subset of C++ C does not have string type – it has its own way of representing strings – we can still use the older way in C++ – called C-strings

C-strings Can recall what string is? – a sequence of zero or more characters we have learned arrays last week – a sequence of variables of the type we define a string can be represented by an array of characters!

C-strings let’s define an array of characters – char s[10]; – 10 character variables created. Now, which strings can be stored in the array? – “” : empty string – “abcde” : the length is 5 – “abcdefghi” : the length is 9 – “abcdefghij” : the length is 10 “abcdefghijklmnopqrstuvwxyz” : the length is 26 (X)

zero byte/null character C-string must end with a marker – the marker indicates “this is the end of the string” – without this marker, it is a character array, not a string – this marker is called zero byte or null character ‘\0’ (ASCII number is 0) – the null character is automatically filled in the last slot

C-strings C String Examples char st1[10] = ""; char st2[10] = "a"; char st3[10] = "abc 123"; a 0 \0 1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 7 ? 8 ? 9 0 ? 1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 7 ? 8 ? 9 a 0 b 1 c ? 8 ? 9

C-strings More C String Examples char st1[] = "abcde"; char st2[] = "abcdefghi"; a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 \0 9 a 0 b 1 c 2 d 3 e 4 5

c-string output int main () { char s[] = "Hello, how are you?"; cout << s <<endl; } Hello, how are you? can use cout << to print out – will print characters until it meets ‘\0’ int main () { char s[] = "Hello, how are you?"; s[5] = '\0'; cout << s <<endl; } Hello

c-string output int main () { char s1[10]="abcde"; char s2[10]="12345"; char c[10]={'1','2','3','4','5'}; int i[10]={1, 2, 3, 4, 5}; double d[10]={1.0, 2.0, 3.0, 4.0, 5.0}; bool b[10]={true,false,true,false,true}; cout << s1 << endl; cout << s2 << endl; cout << c << endl; cout << i << endl; cout << d << endl; cout << b << endl; } abcde F F F88C cout works with character arrays – for other array types, it will print something different

C-string input cin – read one word cin.getline(C-string variable, max) – read up to (max number-1) of characters in one line – ‘\0’ is stored in the last int main () { char s1[20]; char s2[20]; cin >> s1; cin.ignore(10000,'\n'); cin.getline(s2,10); cout << s1 << endl; cout << s2 << endl; } Hi Hi Sungwo Hi Sungwon

initialization vs. assignment int main () { char s[] = "Hello"; cout << s <<endl; } int main () { char s[10]; s = "Hello"; cout << s <<endl; } OK! Compile Error

Quick question what will be displayed? int main () { char s[10] = {'a','b','c','d','e','f','g','h','i'}; cout << s << endl; } abcdefghi int main () { char s[10] = {'a','b','c','d','e','f','g','h','i', 'j'}; cout << s << endl; } abcdefghijts 잔 O 憬 7

More quick question what will be displayed? int main () { char s[10]; s[0] = 'a'; s[1] = 'b'; s[2] = 'c'; s[3] = 'd'; s[4] = 'e'; cout << s << endl; } abcde05"ts?5 int main () { char s[10]; s[0] = 'a'; s[1] = 'b'; s[2] = 'c'; s[3] = 'd'; s[4] = 'e'; s[5] = '\0'; cout << s << endl; } abcde

C-string to C++ strings convert C-string into C++ string type – The C++ string operator = works but, not vice versa int main () { char s[] = "How are you?"; string str = s; cout << str << endl; } How are you? int main () { string str = "How are you?"; char s[] = str; cout << s << endl; } Compile Error

Functions for C-strings basically, C-string is an array of characters – can manipulate characters with index number There are pre-defined functions for C-strings – #include required

C-string libraries strlen(s) – returns the length of s, not counting ‘\0’ int main () { char s[] = "How are you?"; cout << strlen(s) << endl; } 12

C-string libraries strcpy(t, s) – copies the string s to t t = s does not work does not check the array size – make sure there is enough space in t int main () { char s[10] = "Hello"; char t[10]; strcpy(t, s); cout << t << endl; } Hello

C-string libraries strcat(t, s) – concatenate (append) s to the end of t does not check the array size – make sure there is enough space in t int main () { char s[10] = "Hello"; char t[20] = "Sungwon,"; strcat(t, s); cout << t << endl; } Sungwon,Hello

C-string libraries strcmp(t, s) – compare t and s return 0 if they are equal return something greater than 0 if t > s return something less than 0 if t < s int main () { char s[10] = "abcde"; char t[10] = "abcde"; cout << strcmp(t,s) << endl; } 0 int main () { char s[10] = "abcde"; char t[10] = "abced"; cout << strcmp(t,s) << endl; } 1 int main () { char s[10] = "abcde"; char t[10] = "abc"; cout << strcmp(t,s) << endl; }

array of C-strings A C string is essentially an array of characters. – an array of C strings is an array of arrays of characters. An array of arrays is simply a 2D array char s[10][20]; – s[3] : can be used to refer to the string in position 3 – s[3][5] : can be used to refer to the letter in position 5 of the string in position 3. int main () { char s[3][6]; strcpy(s[0], "hello"); strcpy(s[1], "apple"); strcpy(s[2], "world"); cout << s[0] << endl; cout << s[1] << endl; cout << s[2][2] << endl; } hello apple r