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.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
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 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.
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.
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:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
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.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
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.
Chapter 8 Arrays and Strings
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
Fundamental Programming: Fundamental Programming Introduction to C++
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Characters, Strings, And The string Class Chapter 10.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Slide Fundamental Programming Strings.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Fundamentals of Characters and Strings
Arrays in C.
Strings A collection of characters taken as a set:
String in C++.
String What it is Why it’s useful
Chapter 2 Array and String Visit to more Learning Resources.
C++ Programming Lecture 20 Strings
CS31 Discussion 1H Fall18: week 6
Presentation transcript:

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  This week, we will cover the following topics:  Introduction to Character Arrays  Declaring & Initialising Character Arrays  Accessing Character Arrays  String Functions  Two Dimensional Arrays

Array of Characters - Strings  In C++, names, titles, messages, etc, can be held as an array of characters - the term string describes this type of data  In C++, the null character marks the end of a string (the “character” with value 0 in the ASCII coding system - '\0' in C++)  As such, character arrays to hold strings must be declared one element larger than the longest string they will hold

Strings  Newer Compilers eg Visual C #include string myName = “Sue Smith”;  Older Compilers eg Borland Turbo C++ #include char myName[20] = “Sue Smith”;

 The following statement declares an array of characters (MyName) that can hold a string containing up to five characters (including a null character) char MyName[5];  The following statements declare a constant (Size) and an array of characters (Address) that can hold a string containing up to ten characters const int Size=20; char Address[Size]; Declaring Character Arrays

 The following statements declare an array holding four characters – the last being the null character char Company[4] = {'I','B','M','\0'}; char Company[4] = "IBM"; char Company[] = "IBM";  In last two, a C++ string is used to initialise the array – note use of double quotes Initialising Character Arrays

 Initialising an array to the empty string can be achieved by: char Company[25] = "\0"; char Company[25] = {‘\0'};  The former is commonly used Initialising Character Arrays

 If no room is left for the null character, unexpected results/error can be expected char Array1[3] = "111";//Some compilers will give an //error: array bounds overflow error char Array2[3] = "22";//OK char Array3[3] = "33“;//OK  A character array can be printed with cout<<  The statement below cout << “Array2: “ << Array2; will print the following output Initialising Character Arrays Array2: 22

Character Arrays-Example  The following program will print out “Bill” #include void main() { char Name[]=“Bill"; for (int i=0; Name[i] != '\0'; i++) cout << Name[i]; cout << endl; }

Character Arrays-Example  The following program will print out “lliB” #include void main() { char Name[]=“Bill"; for (int i=2; i>=0; i--) cout << Name[i]; cout << endl; }

Character Arrays-Example #include const int size=10; void main() { char Name[size]="ihgfedcba“; char temp; for (int i=0; i<size-1; i++) for (int j=0; j<size-2; j++) if (Name[j]> Name[j+1]) { temp=Name[j]; Name[j]=Name[j+1]; Name[j+1]=temp; } cout << Name << endl; } What does this program do? Output: abcdefghi

String Input and Output  Output is as expected, input is not… #include void main (void) { char Sentence[80] = "\0"; cout "; cin.getline(Sentence,80); cout << ”Sentence: ” << Sentence << endl; }  cin >> stops accepting input when it meets a white space character – getline does not

String Input and Output  Notice that… #include void main (void) { char Sentence[80] = "\0"; cout "; cin.getline(Sentence,80); cout << ”Sentence: ” << Sentence << endl; }  up to 79 characters will be assigned to Sentence – position 79 will hold the null character

getline( ) Function getline(InputArray,ArraySize,UntilChar);  InputArray is a character array variable that will receive the input characters  ArraySize is an integer expression stating the size of the array – (a maximum of ArraySize–1 characters will be assigned to array, followed by null)  UntilChar is an optional parameter; if this character is encountered, input is terminated (‘\n’ is the default)

Other Issues  We cannot simply assign the contents of one character array to another: CharArray1 = CharArray2;  Or combine two character arrays CharArray1 = CharArray2 + CharArray3;  Or compare two character arrays: if (CharArray1 == CharArray2)  Use standard library functions instead

string Library Functions  Assignment and comparison of character arrays is can be accomplished using strcpy(Str1,Str2); // Str2 is copied to Str1 strcat(Str1,Str2); // Str2 is joined to Str1 strcmp(Str1,Str2); // Str2 is compared to Str1 strlen(Str1); // length of Str1  To use these functions, your program must include the string library: #include

strcpy( ) Function Example #include void main (void) { char Array1[] = "111"; char Array2[] = "222"; cout << "Array1: " << Array1 << endl; strcpy(Array1,Array2); cout << "Array1: " << Array1 << endl; } Array1: 111 Array1: 222

strcat( ) Function Example #include void main (void) { char Array1[] = "111"; char Array2[] = "222"; char Array3[7] = "333"; cout << "Array3: " << Array3 << endl; strcat(Array3,Array2); cout << "Array3: " << Array3 << endl; } Array3: 333 Array3:

strcmp( ) Function Example  Using this Compare function void Compare(char Array1[], char Array2[]) { char Relationship = '='; // establish relationship between strings if (strcmp(Array1,Array2) < 0) Relationship = '<'; else if (strcmp(Array1,Array2) > 0) Relationship = '>'; // report relationship between strings cout << "Array1(" << Array1 << ") " << Relationship << " Array2(" << Array2 << ")" << endl; }

strcmp( ) Function Example  The program below #include void Compare(char Array1[], char Array2[]); void main (void) { Compare("111","222"); Compare("111","122"); Compare("abc","ABC"); Compare("ABC","ABC"); Compare("ABCD","ABC"); }

strcmp( ) Function Example  Produces the following output…  Comparison is made using the ASCII value on characters in the string Array1(111) < Array2(222) Array1(111) < Array2(122) Array1(abc) > Array2(ABC) Array1(ABC) = Array2(ABC) Array1(ABCD) > Array2(ABC)

ctype Library Functions  Other useful functions for handling characters and strings are: toascii(Char); // returns ACSII code for char toupper(Char); // returns upper case of char tolower(Char); // returns lower case of char  To use these functions, your program must include the ctype library: #include

toascii( ) Function Example #include void main (void) { cout << "code for 1 is: " << toascii('1') << endl; cout << "code for 2 is: " << toascii('2') << endl; cout << "code for a is: " << toascii('a') << endl; cout << "code for A is: " << toascii('A') << endl; } code for 1 is: 49 code for 2 is: 50 code for a is: 97 code for A is: 65

toupper( ) Function Example #include void main (void) { cout << "upper case of a is: " << toupper('a') << endl; cout << "upper case of a is: " << char (toupper('a')) << endl; cout << "upper case of 1 is: " << char (toupper('1')) << endl; cout << "upper case of A is: " << char (toupper('A')) << endl; } upper case of a is: 65 upper case of a is: A upper case of 1 is: 1 upper case of A is: A

Declaring Two-Dimensional Arrays  The following statement declares a two dimensional array (matrix) that can hold 9 integer values int matrix [3] [3];  matrix – rows & columns: column 0column 1column 2 row 0 matrix[0][0] row 1 row 2 matrix[2][2]

Initialising Two-Dimensional Arrays  int matrix[4][3] = { { 5, 2, 3}, { 4, 2, 6}, { 1, 1, 1}, { 2, 1, 4} };  The declaration double matrix[4][3] = {5}; initialises matrix[0][0] to 5 and all remaining elements to 0.  The declaration double matrix[4][3] = {1, 2, 3, 4}; initialises matrix[0] [0] to 1, matrix[0][2] to 3, matrix[1][0] to 4 and all remaining elements to zero.

Example-add matrix 1 and matrix 2 # include void main () { int matrix1[3][2] = { { 1, 2}, { 6, 2}, { 4, 5}}; int matrix2[3][2] = { { 2, 4}, { 3, 2}, { 4, 1}}; int matrix_res[3][2]; for (int rcount=0; rcount<3; rcount++) { for (int ccount=0;ccount<2; ccount ++) { matrix_res[rcount][ccount]=matrix1[rcount][ccount] + matrix2[rcount][ccount]; cout<< matrix_res[rcount][ccount]; } cout << endl; }

Summary  Character arrays are used to hold strings  Strings terminate in a null zero  So arrays must be of sufficient size to hold the characters in the string, plus one  Many library functions exist to handle strings, eg. strcat, strcpy, strlen, etc.  Newer compilers use the string library  Older compilers use string.h