(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.

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

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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Lecture 2 Introduction to C Programming
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Strings H&K Chapter 9 Instructor – Gokcen Cilingir
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Chapter 10.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 18 I/O in C.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
 Integers and Characters  Character Strings  Input and Output.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
Chapter 15 Strings as Character Arrays
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
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.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
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.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Chapter 18 I/O in C Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, Y. Malaiya Colorado State University.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
C Formatted Input/Output
Chapter 18 I/O in C.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
(1-1) C Review: Pointers, Arrays, Strings, & Structs
Presentation transcript:

(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University

C. Hundhausen, A. O’Fallon 2 String Fundamentals A string is a sequence of characters terminated by the null character (‘\0’) – “This is a string” is considered a string literal – A string may include letters, digits, and special characters A string may always be represented by a character array, but a character array is not always a string A string is accessed via a pointer to the first character in it This week, we'll learn more about how to work with strings in the C Language

C. Hundhausen, A. O’Fallon 3 String Basics (1) Whether you realize it or not, you've been working with C strings all semester: printf("CptS %d is fun!\n",121); It's just that we haven't ever declared a string variable. In C, a string is represented as an array of characters: char name [20]; /* declares a variable name that can hold a string of length 20 */ Be sure to always account for the ‘\0’ in your array declarations name[ ] may have up to 19 characters + 1 for the null character string

C. Hundhausen, A. O’Fallon 4 String Basics (2) As with other data types, we can even initialize a string when we declare it: char name[20] = “Bill Gates"; char *name = “Bill Gates"; char name[] = {‘B’, ‘i’, ‘l’, ‘l’, ‘ ‘, ‘G’, ‘a’, ‘t’, ‘e’, ‘s’, ‘\0’;} // These are equivalent string declarations! Here's what the memory allocated to name looks like after either of the above is executed: B illGates\0????????? name null character (terminates all strings)

C. Hundhausen, A. O’Fallon 5 String Basics (3) Notes on the null character – When a string is initialized on the line it is declared, the compiler automatically "null terminates" the string – All of C's string handling functions work only with null- terminated strings; any characters to the right of the null character are ignored – The ASCII value of the null character is 0

C. Hundhausen, A. O’Fallon 6 String Basics (4) When a variable of type char* is initialized with a string literal, it may be placed in memory where the string can’t be modified If you want to ensure modifiability of a string store it into a character array when initializing it

C. Hundhausen, A. O’Fallon 7 String Basics (5) Populating a string using scanf ( ) char my_string [50]; // The address of operator (&) is not required because the name of the // array is an address scanf (“%s”, my_string); Notes on scanf ( ): Using %s will automatically append a null character to the end of the string Reads character-by-character until whitespace is encountered, i.e. if the user enters: Bill Gates, only “Bill” is read; however, “Gates” is still in the input stream Displaying a string using printf ( ) printf (“%s\n”, my_string); Notes on printf ( ): Using %s will display character-by-character until a null character is encountered; white space and printable special characters will be displayed If a null character is missing from the end of the string, all contiguous memory will be printed until a null character happens to be found in memory

C. Hundhausen, A. O’Fallon 8 String Basics (6) Arrays of Strings – Suppose we want to store a list of students in a class – We can do this by declaring an array of strings, one row for each student name: #define NUM_STUDENTS 5 #define MAX_NAME_LENGTH 31 char student_names[NUM_STUDENTS][MAX_NAME_LENGTH]; – We can initialize an array of strings "in line": char student_names[NUM_STUDENTS][MAX_NAME_LENGTH] = {"John Doe", "Jane Smith", "Sandra Connor", "Damien White", "Metilda Cougar"}; – In most cases, however, we're probably going to want to read the names in from the keyboard or a file…

C. Hundhausen, A. O’Fallon 9 String Basics (7) Printing Out and Reading In Strings #include #define [NUM_STUDENTS] 5 #define [MAX_NAME_LENGTH] 31 char student_names[NUM_STUDENTS][MAX_NAME_LENGTH]; int i; for (i = 0; i < NUM_STUDENTS; ++i) { printf("Please enter student name: "); scanf("%s",student_names[i]); printf("The name '%s' was just read in.\n", student_names[i]); } Is the above code robust? Could it lead to a run-time crash?

C. Hundhausen, A. O’Fallon 10 String Basics (8) – Just as is the case for double s and int s, we can specify a field width in a printf statement involving a string ( %s ). By default, the string is right justified within that field, e.g., printf("string value: %5s\n",my_string); /* string is right justified within field of 5 */ – If we want to left-justify the string, we specify a negative field width, e.g., printf("string value: %-5s\n",my_string); /* string is left justified within field of 5 */

C. Hundhausen, A. O’Fallon 11 String Basics (9) Reading in multiple data types alongside the string data type:

C. Hundhausen, A. O’Fallon 12 String Basics (10) When the previous program is run and the user enters the following (which is not in the correct format): MATH,1270,TR,1800 The scanf call scanf("%s%d%s%d",dept,&course_num,days,&time); interprets this all as one string, storing it to dept (bad news!): Moral: We need a more robust way to read in multiple data types (Stay tuned!)

C. Hundhausen, A. O’Fallon 13 String Basics (11) Example problem: – Write a segment of code that prompts the user for a word of length 24 characters or less, and prints a statement like this: fractal starts with the letter f Have the program process words until it encounters a "word" beginning with the character '9'.

C. Hundhausen, A. O’Fallon 14 String Basics (12) Solution: #include #define STRING_LENGTH 25 int main() { char name[STRING_LENGTH]; int done; do { done = 0; printf("Enter a name ('9') to quit: "); scanf("%s",name); if (name[0] == '9') done = 1; else printf("%s starts with the letter %c.\n", name,name[0]); } while (!done); return (0); }

C. Hundhausen, A. O’Fallon 15 String Basics (13) Use gets() to read a complete line, including whitespace, from the keyboard until the key is pressed; the is not included as part of the string – Usage: gets(my_array) – If the user enters “Bill Gates” and presses, the entire string will be read into my_array excluding the or newline Use puts() to display a string followed by a newline – Usage: puts(my_array)

C. Hundhausen, A. O’Fallon 16 What To Look Forward To… More on Strings: – String handling library functions – Arrays of Pointers – Character input/output and robust string input – Character conversion – String processing example

C. Hundhausen, A. O’Fallon 17 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, P.J. Deitel & H.M. Deitel, C How to Program (7 th Ed.), Pearson Education, Inc., 2013.

C. Hundhausen, A. O’Fallon 18 Collaborators Chris Hundhausen