Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.

Slides:



Advertisements
Similar presentations
Chapter 9 Strings. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives An Array Type for Strings C-Strings Character.
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 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 programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
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 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 21: The Standard Library Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 21 The Standard Library.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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.
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).
Characters and Strings
Today’s Material Strings Definition Representation Initialization
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
CSE 220 – C Programming malloc, calloc, realloc.
C Formatted Input/Output
EGR 2261 Unit 10 Two-dimensional Arrays
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Strings (Continued) Chapter 13
Characters and Strings
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
TMF1414 Introduction to Programming
Strings A string is a sequence of characters treated as a group
Examples Example 1 Demonstrate the use of reference operator
Arrays in C.
Pointers and Arrays Chapter 12
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.
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Lecture 8b: Strings BJ Furman 15OCT2012.
C Arrays.
7 Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Pointers, Dynamic Data, and Reference Types
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Pointers and Arrays Chapter 12
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 16 Pointers and Arrays
CPS120: Introduction to Computer Science
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
CS31 Discussion 1H Fall18: week 6
Characters and Strings
Presentation transcript:

Strings Chapter 13 Copyright © 2008 W. W. Norton & Company. All rights reserved.

Introduction This chapter covers both string constants (or literals, as they’re called in the C standard) and string variables. Strings are arrays of characters in which a special character—the null character—marks the end. The C library provides a collection of functions for working with strings. Copyright © 2008 W. W. Norton & Company. All rights reserved.

String Literals A string literal is a sequence of characters enclosed within double quotes: "When you come to a fork in the road, take it." String literals may contain escape sequences. Character escapes often appear in printf and scanf format strings. For example, each \n character in the string "Candy\nIs dandy\nBut liquor\nIs quicker.\n --Ogden Nash\n" causes the cursor to advance to the next line: Candy Is dandy But liquor Is quicker. --Ogden Nash Copyright © 2008 W. W. Norton & Company. All rights reserved.

How String Literals Are Stored When a C compiler encounters a string literal of length n in a program, it sets aside n + 1 bytes of memory for the string. This memory will contain the characters in the string, plus one extra character—the null character—to mark the end of the string. The null character is a byte whose bits are all zero, so it’s represented by the \0 escape sequence. Copyright © 2008 W. W. Norton & Company. All rights reserved.

How String Literals Are Stored The string literal "abc" is stored as an array of four characters: The string "" is stored as a single null character: Copyright © 2008 W. W. Norton & Company. All rights reserved.

How String Literals Are Stored Since a string literal is stored as an array, the compiler treats it as a pointer of type char *. Both printf and scanf expect a value of type char * as their first argument. The following call of printf passes the address of "abc" (a pointer to where the letter a is stored in memory): printf("abc");  Copyright © 2008 W. W. Norton & Company. All rights reserved.

Operations on String Literals We can use a string literal wherever C allows a char * pointer: char *p; p = "abc"; This assignment makes p point to the first character of the string. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Operations on String Literals String literals can be subscripted: char ch; ch = "abc"[1]; The new value of ch will be the letter b. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Operations on String Literals Attempting to modify a string literal causes undefined behavior: char *p = "abc";   *p = 'd'; /*** WRONG ***/ A program that tries to change a string literal may crash or behave erratically. Copyright © 2008 W. W. Norton & Company. All rights reserved.

String Literals versus Character Constants A string literal containing a single character isn’t the same as a character constant. "a" is represented by a pointer. 'a' is represented by an integer. A legal call of printf: printf("\n"); An illegal call: printf('\n'); /*** WRONG ***/ Copyright © 2008 W. W. Norton & Company. All rights reserved.

String Variables Any one-dimensional array of characters can be used to store a string. A string must be terminated by a null character. Difficulties with this approach: It can be hard to tell whether an array of characters is being used as a string. String-handling functions must be careful to deal properly with the null character. Finding the length of a string requires searching for the null character. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Strings Examples b e y o n d g a m e b e y o n d g a m e b e y o n d g \0 g a m e b e y o n d \0 g a m e \0 b e y o n d g a m e \0 Copyright © 2008 W. W. Norton & Company. All rights reserved.

String Variables If a string variable needs to hold 80 characters, it must be declared to have length 81: #define STR_LEN 80 … char str[STR_LEN+1]; Adding 1 to the desired length allows room for the null character at the end of the string. Defining a macro that represents 80 and then adding 1 separately is a common practice. Copyright © 2008 W. W. Norton & Company. All rights reserved.

String Variables Be sure to leave room for the null character when declaring a string variable. Failing to do so may cause unpredictable results when the program is executed. The actual length of a string depends on the position of the terminating null character. An array of STR_LEN + 1 characters can hold strings with lengths between 0 and STR_LEN. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Initializing a String Variable A string variable can be initialized at the same time it’s declared: char date1[8] = "June 14"; The compiler will automatically add a null character so that date1 can be used as a string:   "June 14" is not a string literal in this context. Instead, C views it as an abbreviation for an array initializer. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Initializing a String Variable If the initializer is too short to fill the string variable, the compiler adds extra null characters: char date2[9] = "June 14"; Appearance of date2: Copyright © 2008 W. W. Norton & Company. All rights reserved.

Initializing a String Variable An initializer for a string variable can’t be longer than the variable, but it can be the same length: char date3[7] = "June 14"; There’s no room for the null character, so the compiler makes no attempt to store one: Careful here  Copyright © 2008 W. W. Norton & Company. All rights reserved.

Initializing a String Variable The declaration of a string variable may omit its length, in which case the compiler computes it: char date4[] = "June 14"; The compiler sets aside eight characters for date4, enough to store the characters in "June 14" plus a null character. Omitting the length of a string variable is especially useful if the initializer is long, since computing the length by hand is error-prone. Copyright © 2008 W. W. Norton & Company. All rights reserved.