20061213 chap9 Chapter 9 Strings. 20061213 chap9 2 Strings A data structure deals with a grouping of characters. C implements the string data structure.

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

Problem Solving & Program Design in C
Introduction to C Programming
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
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.
Chapter 9 Character Strings
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Topic 10 - Strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of.
CS 201 String Debzani Deb.
CS Nov 2006 C-strings.
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Characters and 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.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Introduction to C programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
Chapter 8 Arrays and Strings
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Characters and Strings File Processing Exercise C Programming:Part 3.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
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:
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
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).
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
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.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Problem Solving and Program Design in C Chap. 8 Strings Chow-Sing Lin.
C Characters and Strings
Strings CSCI 112: Programming in C.
C Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
A First Book of ANSI C Fourth Edition
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
Chapter 8 Character Arrays and Strings
Lec 11.
Characters and Strings Functions
C Characters and Strings
Computer Science II CS132/601* Lecture #C-3.
Presentation transcript:

chap9 Chapter 9 Strings

chap9 2 Strings A data structure deals with a grouping of characters. C implements the string data structure using arrays of type char. Numerical data & textural data. Typewriter  Computer-based word processing systems You have already used the string extensively. printf(“This program is terminated!\n”); #define ERR_Message “Error!!”

chap9 3 Declaring & Initializing String Variables string Since string is an array, the declaration of a string is the same as declaring a char array. char string_var[30]; char string_var[20] = “Initial value”; null character ‘ \0 ’ The string is always ended with a null character ‘ \0 ’. The characters after the null character are ignored. nitial value??…I \0 [0] [13]

chap9 4 Arrays of Strings One string is an array of characters. An array of strings is a two-dimensional array of characters in which each row is one string. char names[People][Length]; char month[5][10] = {“January”, “February”, “March”, “April”, “May”};

chap9 5 I/O with printf and scanf %s The placeholder %s is used to represent string arguments in printf and scanf. printf(“Topic: %s\n”, string_var); The string can be right-justified by placing a positive number in the placeholder. %8s printf(“%8s”, str); The string can be left-justified by placing a negative number in the placeholder. %-8s printf(“%-8s”, str);

chap9 6 Right- and Left-Justified Strings %8s The “ %8s ” placeholder displays a string which is right-justified and in 8-columns width. If the actual string is longer than the width, the displayed field is expanded with no padding.

chap9 7 Example (Fig. 9.2) The dept is the initial memory address of the string argument. Thus we don’t apply the & operator on it.

chap9 8 Execution of scanf ("%s", dept); Whenever encountering a white space, the scanning stops and scanf places the null character at the end of the string. e.g., if the user types “ MATH 1234 TR 1800, ” the string “ MATH ” along with ‘ \0 ’ is stored into dept.

chap9 9 Entry of Invalid Data scanf("%s%d%s%d", dept, &course_num, days, &time); > MATH,1270,TR,1800

chap9 10 String Library Functions The string can not be copied by the assignment operator ‘ = ’. str = “Test String” --- not valid. C provides string manipulating functions in the “ string.h ” library. The complete list of these functions can be found in Appendix B of the textbook.

chap9 11 String Library Functions from string.h

chap9 12 String Assignment Function strcpy copies the string in the second argument into the first argument. char one-str[20]; strcpy(one_str, “test string”); null character The null character is appended at the end automatically. If source string is longer than the destination string, the overflow characters may occupy the memory space used by other variables. BAD: The value of these other variables would seem to change spontaneously. On rare occasions, such overflow would generate a run-time error message. strcpy(one_str, “A very long test string”);

chap9 13 strcpy vs. strncpy Function strncpy copies the string by specifying the number of characters to copy. If source string is longer than the destination string, the overflow characters are discarded automatically. strncpy(dest, source, dest_len-1); = ‘ \0 ’ ; dest[dest_len-1] = ‘ \0 ’ ; //To place the null char. manually.

chap9 14 Substrings We frequently need to reference a substring of a longer character string. We can use strncpy to copy a middle or an ending portion of a string. char *strncpy(char *dest, const char *source, size_t n);

chap9 15 Example of substring char result[10]; char s1[15] = “Jan. 30, 1996”; strncpy(result, s1, 9); results[9]= ‘\0’;

chap9 16 Another example of substring strncpy(result, &s1[5], 2); results[2]= ‘\0’;

chap9 17 Separate compounds into elemental components (Fig. 9.7)

chap9 18 String Concatenation Functions strcat and strncat concatenate the fist string argument with the second string argument. char f1[15] = “John ”, f2[15] = “Jacqueline ”, last[15] = “Kennedy”; strcat(f1, last); strcat(f2, last); /* invalid overflow */ strncat(f2, last, 3);

chap9 19 String Length When writing a string-manipulating program, one usually does not know in advance the sizes of the string used as data. Function strlen is often used to check the length of a string (i.e., the number of characters before the first null character). dest[6] = “Hello”; strncat(dest, “more”, 5-strlen(dest)); dest[5] = ‘\0’;

chap9 20 Characters vs. Strings The representation of a char (e.g., ‘ Q ’ ) and a string (e.g., “ Q ” ) is essentially different. A string is an array of characters ended with the null character. Q Character ‘Q’ Q\0 String “Q”

chap9 21 String Comparison Suppose there are two strings, str1 and str2. initial memory address The condition str1 < str2 compare the initial memory address of str1 and of str2. The comparison between two strings is done by comparing each corresponding character in them. The characters are compared against the ASCII table. “thrill” < “throw” since ‘i’ < ‘o’ “joy” < “joyous” The standard string comparison uses the strcmp and strncmp functions.

chap9 22 String Comparison RelationshipReturned Value Example str1 < str2 Negative “ Hello ” < “ Hi ” str1 = str2 0 “ Hi ” = “ Hi ” str1 > str2 Positive “ Hi ” > “ Hello ” e.g., we can check if two strings are the same by if(strcmp(str1, str2) != 0) printf(“The two strings are different!”);

chap9 23 Numeric and String Versions of Portions of Selection Sort That Compare and Exchange Elements

chap9 24 Sentinel-Controlled Loop for String Input

chap9 25 Arrays of Pointers

chap9 26 Arrays of Pointers char *alphap[5];

chap9 27 I/O of Characters and Strings The stdio library provides getchar function which gets the next character from the standard input. “ ch = getchar(); ” is the same as “ scanf(“%c”, &ch); ” Similar functions are putchar, gets, puts. For IO from/to the file, the stdio library also provides corresponding functions. getc : reads a character from a file. Similar functions are putc, fgets, fputs.

chap9 28 Character Analysis and Conversion The library defines facilities for character analysis and conversion. FunctionsDescription isalpha Check if the argument is a letter isdigit Check if the argument is one of the ten digits isspace Check if argument is a space, newline or tab. tolower Converts the lowercase letters in the argument to upper case letters.

chap9 29 String-to-Number and Number-to-String The defines some basic functions for conversion from strings to numbers: atoi( “ 123 ” ) atoi( “ 123 ” ) converts a string to an integer. atol( “ 123 ” ) atol( “ 123 ” ) converts a string to a long integer. atof( “ 12.3 ” ) atof( “ 12.3 ” ) converts a string to a float. However, there is no functions such as itoa, itoa, … etc, sprintf because there is a function called sprintf which can converts many formats to a string.

chap9 30 sprintf and sscanf sprintf printf The sprintf function substitutes values for placeholders just as printf does except that it stores the result into a character array sprintf(s, “%d%d%d”, mon, day, year); sscanfscanf The sscanf function works exactly like scanf except that it takes data from the string as its input argument. sscanf(“ Hello”, “%d%lf%s”, &num, &val, word);

chap9 31 Homework #9 Due: 2006/12/27 讀 / 寫 檔案,並作字串的處理 提供一個已經加密過的檔案 problem_encode.txt,內藏有一些 算式 請將此檔案解密後, 再將此檔案 內的算式算出,並做出加總

chap9 32 Demo: Text Editor

chap9 33 Summary