N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
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.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
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:
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Characters and Strings 4 Character constants (single quotes): –‘a’, ‘A’, ‘0’, ‘1’, ‘\n’, ‘\0’, … 4 String constants (double quotes): –“Mary had a little.
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
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 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Introduction to C programming
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
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’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
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.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
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.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
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.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
Arrays in C.
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
INC 161 , CPE 100 Computer Programming
Strings What is a string? It is an array of characters terminated with
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
Introduction to Problem Solving and Programming
Presentation transcript:

N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE

N-2 Overview Concepts this lecture String constants Null-terminated array representation String library String initializers Arrays of strings

N-3 Chapter 9 Read Sections 9.1, 9.2, and 9.4: 9.1: String Basics Table 9.1 for summary of common functions 9.2: String Assignment 9.4: String Comparison

N-4 Character Data in Programs Names, messages, labels, headings, etc. All of these are common in computer applications All involve characters: usually multiple characters So far, our ability to handle these things in C is very limited

N-5 Characters and Strings Character constants (literals): single quotes ‘a’, ‘A’, ‘0’, ‘1’, ‘\n’, ‘ ’, ‘B’, ‘i’, ‘l’, ‘\0’ null character String constants (literals): double quotes "Bill is very rich" “The answer is %.2f. \n"

N-6 String Representation (1) Strings are stored in char arrays Programming convention: a null character ‘\0’ is stored at the end string representation "sample" sample \0

N-7 String Representation (2) ‘\0’ included in string constants automatically Programmer must take pains to be sure it is present elsewhere when needed sample \0

N-8 String Representation (3) Character arrays holding strings must have room for ‘\0’ following the actual data The empty string "" occupies 1 char Character and string constants are not the same: ‘x’ and "x" are different. How? sample \0

N-9 String Operations Common needed operations: Copy (assignment) Compare Find length Concatenate (combine strings) I/O Unfortunately... sample \0

N-10 What You Can’t Do Strings are arrays They have the limitations of arrays Can’t assign one string to another with = Can’t compare strings with ==, <= But there are library functions to help do such things sample \0

N-11 String Library: Standard C includes a library of string functions use #include Library functions: Require proper null-terminated (‘\0’) strings as arguments Produce null-terminated strings as results (usually) sample \0

N-12 String Length: strlen strlen returns the length of its string argument Does not count the null ‘\0’ at the end Examples: The length of "A" is 1 The length of "" is 0 k = strlen("null-terminated string"); stores 22 in k

N-13 /* * return the length of string s, i.e., * number of characters before terminating '\0', * or equivalently, index of first '\0'. */ int strlen( char s[ ] ) { int n = 0; while ( s[n] != '\0') n = n + 1 ; return n; } A strlen implementation

N-14 String Assignment: strcpy strcpy(dest, source); Copies characters from source to dest Copies up to, and including the first ‘\0’ found Be sure that dest is large enough to hold the result!

N-15 String Assignment: Examples #include... char medium[21] ; char big[1000] ; char small[5] ; strcpy(medium, "Four score and seven" ) ; medium: Four score and seven \0

N-16 String Assignment: Examples char medium[21 ]; char big[1000] ; char small[5] ; strcpy(big, medium) ; strcpy(big, "Bob") ; big: Four score and seven \0 ?????... big: Bob \0 score and seven \0 ?????...

N-17 String Assignment Dangers char medium[ 21]; char big[1000] ; char small[5] ; strcpy(small, big) ; strcpy(small, medium) ; /* looks like trouble... */ small: Bob \ 0 ? small: Four score and seven \0

N-18 A strcpy implementation /* copy source string into dest, stopping with '\0' */ void strcpy(char dest[ ], char source[ ]) { int i = 0; while (source[ i ] != ‘\0’) { dest[ i ] = source[ i ] ; i ++; } dest[ i ] = ‘\0’ ; }

N-19 String Concatenation: strcat To append means to place one string directly after another "chop" appended to "lamb" should result in "lambchop" strcat(dest, source); Appends characters from source to dest Copy is stored starting at first ‘\0’ in dest Copies up to, and including the first ‘\0’ in source Be sure that dest is large enough!

N-20 Using strcat (1) #include... char str1[5], str2[5], str3[11]; strcpy(str1, "lamb"); strcpy(str2, "chop"); str1 ? ? ? ? ? str3 ? ? ? ? ? ? ? ? ? ? ? str2 ? ? ? ? ? c h o p \0 l a m b \0

N-21 Using strcat (2) strcpy(str3, str1); strcat(str3, str2); str1 l a m b \0 str3 ? ? ? ? ? ? ? ? ? ? ? str2 c h o p \0 l a m b \0 c h o p \0

N-22 String Comparison: strcmp strcmp(s1, s2); Compares s1 to s2 and returns an int describing the comparison Negative if s1 is less than s2 Zero if s1 equals s2 Positive if s1 is greater than s2

N-23 Comparing Strings strcmp compares corresponding characters until it finds a mismatch. "lamb" is less than "wolf" "lamb" is less than "lamp" "lamb" is less than "lambchop"

N-24 Using strcmp (1) Don't treat the result of strcmp as a Boolean! Test the result as an integer if (strcmp(s1,s2) == 0) printf("same\n");

N-25 Using strcmp (2) If you treat the result of strcmp as a Boolean, it probably won’t do what you want if (strcmp(s1,s2)) printf("yikes!"); prints yikes if s1 and s2 are different!

N-26 String I/O scanf and printf can read and write C strings Format code is %s ‘\0’ termination handled properly Be sure there’s enough space for data plus ‘\0’ on input! #define MAX_INPUT 2000 char buffer [MAX_INPUT]; … scanf("%s", buffer);

N-27 Many Functions in strcat, strncatconcatenation strcmp, strncmpcomparison strtod, strtol, strtoulconversion Lots of others: see Appendix B. Related useful functions in operations on a single char: convert case, check category, etc. See a textbook or reference manual

N-28 Many Functions in strcat, strncatconcatenation strcmp, strncmpcomparison strtod, strtol, strtoulconversion Lots of others: check your favorite reference. Related useful functions in operations on a single char: convert case, check category, etc.

N-29 Using Libraries of Functions To use strings effectively in C, use functions from string.h Using libraries is very typical of C programming ANSI C standard libraries such as stdio.h, string.h, ctype.h Application-specific libraries: (thousands of them exist) You can’t be an effective programmer without being able to quickly master new libraries of functions

N-30 Bonus: String Initializers char pet[5] = { ‘l’, ‘a’, ‘m’, ‘b’, ‘\0’ } ; char pet[5] ; pet[0] = ‘l’ ; pet[1] = ‘a’ ; pet[2] = ‘m’ ; pet[3] = ‘b’ ; pet[4] = ‘\0’ ; char pet[5] = "lamb" ; But not: char pet[5]; pet = “lamb” ; /* No array assignment in C */ Remember that initializers are not assignment statements! all equivalent

N-31 Bonus: Arrays of Strings char month[12][10] = { "January", "February",... "September",/* longest month: 9 letters */... "December" } ;... printf ("%s is hot \n", month[7] ); /* August */

N-32 Strings Summary Definition: Null-terminated array of char Strings are not fully a type of C They share most limitations of arrays scanf/printf: %s library functions Assignment: strcpy Length: strlen strcat and many others Major Pitfall: overrunning available space