Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Slides:



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

 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 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
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.
Pointers in C Rohit Khokher
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
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.
Topic 10 - Strings.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
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.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
Chapter 9: Arrays and Strings
Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Introduction to C programming
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
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.
February 14, 2005 Characters, Strings and the String Class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 8 Arrays and Strings
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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’};
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Introduction to C++ Part II Version 1.3. Topics C++ Functions -- passing by value in C++ -- passing by reference in C++ -- passing by address in C++ C++
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
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.
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Fundamentals of Characters and Strings
C++ Programming Lecture 20 Strings
Introduction to Problem Solving and Programming
Presentation transcript:

Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

2 A array is a collection of variables of the same data type that are referred to by a common name. A array is a collection of variables of the same data type that are referred to by a common name. Arrays may have from one to several dimensions, but the one- dimensional array is the most common. Arrays may have from one to several dimensions, but the one- dimensional array is the most common. Arrays are convenient for grouping related variables together. Arrays are convenient for grouping related variables together. A character array is very commonly used because it can hold strings. A character array is very commonly used because it can hold strings. Remember that C/C++ does not define a built-in string data type (C++ does implement a string class), but implements them as null terminated arrays of characters. Remember that C/C++ does not define a built-in string data type (C++ does implement a string class), but implements them as null terminated arrays of characters. What are Arrays?

3 Review of Characters in Memory You can represent a single character in code by 'B'. Note the use of single quotes around the character. You can represent a string in code by "Hello World". Note the use of double quotes around the character(s). In memory each character is stored in a single byte. In memory each character of a string is stored in a single byte and it is terminated with a NULL character. Example: \0dlroW olleH B Memory Character String NULL Character

4 One-Dimensional Arrays A one-dimensional array is a list of related variables. Its general format is: The type declares the base type for the elements of the array and size defines the number of elements in the array. For example, the following code defines an array containing 10 integers. To index value must be used to access an individual element of an array. An index describes the position (relative to zero) of an element in an array. In C/C++ ALL arrays are zero based! type var-name[size]; int sample[10];

5 One-Dimensional Arrays (continued) Here is a sample program that defines, loads, and then displays the contents of an array. #include using namespace std; int main() { int sample[10]; // this reserves 10 integer elements int t; for( t = 0; t < 10; ++t ) // load the array sample[t] = t; for( t = 0; t < 10; ++t ) // display the array cout << sample[t] << ' '; return 0; } All arrays occupy contiguous memory locations.

6 One-Dimensional Arrays (continued) This example creates an array of 8 integers. Each element is identified by its index number. The valid indexes for this array are 0 – 7. To reference/access the 4 th element of this array you must use the following: What does this do? Array x with 8 elements. Index 0 Index 7 int x[8]; // defines an array of 8 integers cout << x[3] << endl; // Why the 3? x[5] = 3467; x[0] x[7]x[6]x[5]x[4]x[3]x[2]x[1] total bytes = number of bytes in type x number of elements

7 One-Dimensional Arrays (continued) To process the data stored in an array, you reference each individual element by specifying the array’s name and the index number of the element. This example declares an integer array and initializes it: In memory the array would look like this. Array X with 2 elements. Index int X[2]; // defines an array of two integers X[0] = 12; // sets first element to 12 X[1] = 135; // sets second element to 135

8 One-Dimensional Arrays (continued) Here is a program that defines an array of 10 elements, assigns each element to a random value, and displays the minimum and maximum values. #include using namespace std; int main() { int i, min_value, max_value; int list[10]; // define an array of 10 integers for(i = 0; i < 10; i++) list[i] = rand(); min_value = list[0]; // find minimum value for(i = 1; i < 10; i++) if( min_value > list[i] ) min_value = list[i]; cout << "minimum value: " << min_value << endl'; max_value = list[0]; // find maximum value for(i = 1; i < 10; i++) if( max_value < list[i] ) max_value = list[i]; cout << "maximum value: " << max_value << endl; return 0; }

9 Lab 1 Code: #include using namespace std; void main() { int Ary[4]; for (int x = 0; x < 4; x++) { cout << "Enter a whole number: "; cin >> Ary[x]; } for (int y = 0; y < 4; y++) cout << "Element " << y << " contains " << Ary[y] << endl; }

10 Arrays You CANNOT assign one array to another array. The following is illegal. You must manually copy each element of an array to the other array as follows: for( int i = 0; i < 10; i++) a[i] = b[i]; int a[10]; int b[10]; a = b; // error -- illegal

11 No Bounds Checking C/C++ performs NO bounds checking on arrays. Nothing prevents you from overrunning the end of an array. It is your responsibility to make sure this does not happen. int y[8]; for( int i = 0; i < 10; i++) a[i] = i; This memory has been overlaid. NOT GOOD Memory allocated for the array All bets are now off concerning the well being of the program’s execution.

12 Sorting Arrays Sorting the data contained in arrays is a common operation. There are several type of sorts you can use: Quicksort Quicksort Shaker sort Shaker sort Shell sort Shell sort Bubble sort Bubble sort Since sorting arrays is so common, a standard library function is provided which implements the Quicksort. It is one of the fastest general purpose sorting routines available. The text covers the Bubble sort, but we will learn to use the qsort() library function. This function is designed to sort an array of any type data in either ascending or descending sequence.

13 The qsort() Function Format: The qsort() function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array (usually the first element of the array) to be sorted. compare is the name of a function, which MUST be provided by you, that controls the sequence of the elements (ascending or descending). It is called automatically by the qsort() function and NOT by you. qsort() overwrites this array with the sorted elements. void qsort( void *base, // Start of target array size_t num, // number of elements to sort size_t width, // size in bytes of each element // compare function used by the qsort() (int (*)(const void *, const void *)) compare )

14 The qsort() Function (continued) The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort() calls the compare routine one or more times during the sort, passing pointers to two array elements on each call: The routine must compare the elements, then return one of the following values: <0 if elem1 is less than elem2 <0 if elem1 is less than elem2 0 if elem1 is equal to elem2 0 if elem1 is equal to elem2 >0 if elem1 is greater than elem2 >0 if elem1 is greater than elem2 The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function. compare( (void *) elem1, (void *) elem2 );

15 Example of Using the qsort() Function #include using namespace std; int Numeric(int *p1, int *p2) { if (*p1 < *p2 ) return -1; if (*p1 == *p2 ) return 0; return 1; } int main() { int A[5] = {345, 90000, 1, 56, 4000}; int i; for(i = 0; i < 5; i++) // Display array before sorting cout << setw(8) << A[i]; cout << endl; qsort(&A, 5, sizeof(A[0]),(int(*) (const void *, const void *)) Numeric); for(i = 0; i < 5; i++) // Display sorted array cout << setw(8) << A[i]; cout << endl; return 0; }

16 The qsort() Function (continued) OUTPUT: The sizeof() function returns the number of bytes of memory allocated for the variable. int x; int y[10]; int size; char lastName[17]; cout << sizeof( x ); // displays 4 cout << sizeof( y ); // displays 40 size = sizeof( y[2] ); // sets size to 4 cout << sizeof( lastName ); // displays 17 cout << sizeof( int ); // displays 4 cout << sizeof( double ); // displays 8

17 Strings C/C++ does not have a string data type. In C/C++, a string is defined as a character array terminated by a null (‘\0’). Because of this null terminator, you will need to take this into consideration when calculating the number of characters it will take to store the string. Examples: The last name field can contain a maximum of 15 characters. You can NOT do a direct assignment of strings like: You can NOT do a direct assignment of strings like: You can initialize a string variable at the declaration: SamSmith\0?????? Always add 1 to the total number of characters to account for the NULL terminator. char Lname[16]; Lname = "Sam Smith"; // this is illegal char Lname[16] = "Sam Smith";

18 Strings (continued) What does this create? This creates an empty string or null string. Since strings are implemented as a one-dimensional array of char’s, we can access any individual character in the string by using an index value. \o??????? char str[] = “ABCDEFGHI”; cout << str[3];// displays D char Str[8] = “”;

19 Reading a String from the Keyboard The easiest way is to define a string variable and use the cin statement to input data from the keyboard into it. #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; cin >> str; // read string from keyboard cout << "Here is your string: "; cout << str; return 0; } Sample run: Enter a string: This is a test Here is your string: This

20 Reading a String from the Keyboard Do you see the problem? Only the word This is inputted by the cin statement. The reason for this is that the cin statement STOPS inputting characters from the keyboard buffer when a whitespace character is encountered. Whitespace characters are: spaces spaces tabs tabs newlines newlines One solution to this problem is to use the gets() library function. Its format is: gets(array-name);

21 Reading a String from the Keyboard Here is the same program from above, but it uses the gets() function instead of the cin statement. #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; gets(str); // read a string from the keyboard cout << "Here is your string: "; cout << str; return 0; } Sample run: Enter a string: This is a test Here is your string: This is a test

22 Reading a String from the Keyboard For reasons we will cover later, the name of a character array that holds a string can be used any place that a string literal can be used. Remember: Neither the cin or the gets() performs any boundary checking on the array.

23 String Library Functions The cstring library holds the functions for string manipulation. Some of the more common string library functions are: strcpy( dest, source ); strncpy( dest, source, number ); strcat( dest, source ); strncat( dest, source, number ); strcmp( str1, str2 ); strncmp( str1, str2, number ); strlen( source );

24 The strcpy() Function Format: This function makes a copy of source string and places it into the dest string. strcpy(dest, source); #include using namespace std; int main() { char str[80]; strcpy(str, "hello"); cout << str; return 0; } OUTPUT: hello

25 The strncpy() Function Format: This function makes a copy of a specific number of characters in source string and places it into the dest string. strncpy(dest, source, number); #include using namespace std; int main() { char s1[20]; char s2[] = "ABCDEFGHIJKLM"; strncpy(s1, s2, 6); cout << s1; return 0; } OUTPUT: ABCDEF

26 The strcat() Function Format: This function appends the source string to the end of the dest string. This is called concatenation. strcat(dest, source); #include using namespace std; int main() { char s1[20], s2[10]; strcpy(s1, "hello"); strcpy(s2, " there"); strcat(s1, s2); cout << s1; return 0; } OUTPUT: hello there

27 The strncat() Function Format: This function appends a specific number of characters in source string to the end of the dest string. strncat( dest, source, number ); #include using namespace std; int main() { char s1[20] = "ABCDEF" char s2[10] = "WXYZ"; strncat( s1, s2, 2 ); cout << s1; return 0; } OUTPUT: ABCDEFWX

28 The strcmp() Function Format: This function compares two strings. It returns the following: a negative value if str1 < str2 a negative value if str1 < str2 a zero if the strings are equal a zero if the strings are equal a positive value if str1 > str2. a positive value if str1 > str2. strcmp( str1, str2 );

29 #include using namespace std; bool password(); int main() { if( password() ) cout << "Logged on.\n"; else cout << "Access denied.\n"; return 0; } bool password() { // Return true if password accepted; false otherwise. char s[80]; cout << "Enter password: "; gets(s); if( strcmp( s, "password" ) ) { // strings differ cout << "Invalid password.\n"; return false; } return true; }

30 The strncmp() Function Format: This function compares a specified number of characters in str1 to the same number of characters in str2. It returns the same values as strcmp(). strncmp( str1, str2, number ); #include using namespace std; int main() { char str1[] = "ABCJKL"; if( strncmp( str, "ABC", 3 ) ) cout << "Found a match.\n"; else cout << "Match not found.\n"; return 0; } OUTPUT: Found a match

31 The strlen() Function Format: This function returns the number of characters a string currently contains excluding the NULL character. strlen(source); #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; gets(str); cout << "Length is: " << strlen(str) << endl;; return 0; } SAMPLE EXECUTION: Enter a string: Hello world Length is: 11

32 The strlen() Function (continued) The strlen() function is a very useful function for processing the individual characters of a string variable. Here is an example: #include using namespace std; int main() { // Print a string backwards. char str[80]; int i; cout << "Enter a string: "; gets(str); for( i = strlen(str) - 1; i >= 0; i-- ) cout << str[i]; return 0; }

33 Another strlen() Function Example #include using namespace std; int main() { // Scan a string to see if it contains only digits. char str[80]; int i; bool good = true; cout << "Enter a string: "; gets(str); for( i = 0; i < strlen(str); i++ ) if( str[i] ‘9’ ) { good = false; break; } if( good ) cout << “String contains only numbers” << endl; else cout << “Illegal character found: ” << str[i] << endl; return 0; }

34 Another strlen() Function Example #include using namespace std; int main() { // Test for an overflow condition. char str[80]; cout << "Enter a string: "; gets(str); if( strlen( str ) > 79 ) { cout << “Overflow condition has occurred” << endl; // this statement terminates the program exit( EXIT_FAILURE ); } return 0; }

35 Sample Code #include using namespace std; char Source1[6] = "Hello"; char Source2[12] = "Hello Tommy"; char Dest[15] = "\0"; int main() { cout << "The contents of Dest is " << Dest << ".\n\n"; cout << "Use strcpy to copy Source1 to Dest\n"; strcpy( Dest, Source1); cout << "The contents of Dest is " << Dest << ".\n\n"; cout << "Use strncpy to copy Source2 to Dest\n"; strncpy( Dest, Source2, 9); cout << "The contents of Dest is " << Dest << ".\n\n"; cout << "Use strcat to append ' again' to Dest\n"; strcat( Dest, " again "); cout << "The contents of Dest is " << Dest << ".\n\n";

36 cout << "Use strncat to append Source2 to Dest\n"; strncat( Dest, Source2, 9); cout << "The contents of Dest is " << Dest << ".\n\n"; cout << "Use strcmp to compare Source1 to Source2\n"; if (strcmp( Source1, Source2) == 0) cout << "The two strings are equal.\n\n"; else if (strcmp( Source1, Source2) < 0) cout << "Source1 should pre ceed Source2.\n\n"; else if (strcmp( Source1, Source2) > 0) cout << "Source2 should preceed Source1.\n\n"; cout << "Use strncmp to compare Source1 to Source2\n"; if (strncmp( Source1, Source2, 5) == 0) cout << "The two strings are equal.\n\n"; else if (strncmp( Source1, Source2, 5) < 0) cout << "Source1 should preceed Source2.\n\n"; else if (strncmp( Source1, Source2, 5) > 0) cout << "Source2 should preceed Source1.\n\n"; }

37 OUTPUT: The contents of Dest is. Use strcpy to copy Source1 to Dest The contents of Dest is Hello. Use strncpy to copy Source2 to Dest The contents of Dest is Hello Tom. Use strcat to append ' again' to Dest The contents of Dest is Hello Tom again. Use strncat to append Source2 to Dest The contents of Dest is Hello Tom again Hello Tom. Use strcmp to compare Source1 to Source2 Source1 should preceed Source2. Use strncmp to compare Source1 to Source2 The two strings are equal. Press any key to continue

38 Arrays Of Strings You can create a two dimensional array of strings. #include using namespace std; int main() { // The first number is how many rows the array has and the // second number is how many characters each record contains. char Days[7][10] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday“ }; for( int loop = 0; loop < 7; loop++ ) cout << Days[loop] << "\n"; return 0; }

39 Arrays Of Strings (continued) OUTPUT: Monday Tuesday Wednesday Thursday Friday Saturday Sunday Press any key to continue

40 Arrays Of Strings (continued) MONDAY\0 TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY [0][0] [0][1][0][2][0][3][0][4][0][5][0][6][0][7][0][8][0][9] [1][0] [2][0] [3][0] [4][0] [5][0] [6][0] char Days[7][10] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday“ }; char Days[7][] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday“ }; These are equivalent:

41 Arrays You can initialize an array when it is declared. Example: You can also create an array and initialize it without giving the number of elements it will be. Example: int X[4] = {2, 5, 23, 177}; int X[] = {2, 5, 23, 177};

42 Sample Array #include using namespace std; void main() { char monthNames[12][] = { “January”, “February“, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” }; int month; cout << “Enter month number ( 1 – 12 ): “; cin >> month; cout << “The month is: “ << monthNames[month] << endl; return 0; } SAMPLE EXECUTION: Enter month number ( 1 – 12 ): 10 The month is: November What is the problem?

43 Corrected Sample Array #include using namespace std; void main() { char monthNames[12][] = { “January”, “February“, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” }; int month; cout << “Enter month number ( 1 – 12 ): “; cin >> month; cout << “The month is: “ << monthNames[month - 1] << endl; return 0; } SAMPLE EXECUTION: Enter month number ( 1 – 12 ): 10 The month is: October