Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) 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

Introduction to C Programming
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
 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.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 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
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 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.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 4 Outline 4.0 Reading.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
 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.
CHAPTER 8 CHARACTER AND STRINGS
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State 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’};
Chapter 3 Input and Output
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
(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.
CSC Programming for Science Lecture 8: Character Functions.
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
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Files A collection of related data treated as a unit. Two types Text
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Strings CSCI 112: Programming in C.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
C Characters and Strings
Chapter 7 Text Input/Output Objectives
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 18 I/O in C.
Plan for the Day: I/O (beyond scanf and printf)
Chapter 8 - Characters and Strings
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
File Input and Output.
Module 12 Input and Output
C Characters and Strings
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University

2 Character Operations (1) Up to now, we filled string variables by reading formatted data from standard input or from a file via scanf and fscanf calls. As demonstrated earlier, these functions may not be the best choice for some text processing applications. Recall the example: scanf("%s%d%s%d",dept,&course_num,days,&time); When we need custom formatting or we don’t want to deal with the situations where user errors leads to run time errors, we may want to get our hands dirty and use functions that read and write individual characters, rather than entire strings. #include directive will give us access to several functions that operate on individual characters, which may be an asset for us when we’re dealing with individual characters for text processing

3 Character Operations (2) int getchar() Returns the next available character from the standard input Equivalent to the following call: scanf("%c",&ch), except that the character is returned as the function result (actually the integer (ASCII) code is returned as integer) If you assign what getchar returns to a variable of type char, automatic conversion will take place. If the End Of File is reached or a reading error happens, the function returns EOF (-1)

4 Character Operations (3) int getc(FILE *stream) Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced by one character to point to the next character. Equivalent to the following call: fscanf(stream, "%c",&ch), except that the character is returned as the function result (actually the integer (ASCII) code is returned as integer) If the End Of File is reached or a reading error happens, the function returns EOF (-1)

5 Character Operations (4) int putchar(int character) Writes character to the current position in the standard output and advances the internal file position indicator to the next position. Equivalent to the following call: printf("%c",ch) Note that since automatic conversion takes place between integers and characters, it is OK to call putchar with a character, e.g., putchar('s');

6 Character Operations (5) int putc(int character, FILE *stream) Writes character to the stream and advances the position indicator. The character is written at the current position of the stream as indicated by the internal position indicator, which is then advanced one character. Equivalent to the following call: fprintf(stream, "%c",ch) Note that since automatic conversion takes place between integers and characters, it is OK to call putc with a character, e.g., putc('s');

C. Hundhausen, A. O’Fallon7 Character Operations (6) provides several functions that help us to classify characters, which you’ve already seen: ◦ int isalpha(int) – is the character a letter of the alphabet? ◦ int isdigit(int) – is the character one of the ten decimal digits? ◦ int islower(int) – is the character a lowercase letter? ◦ int isupper(int) – is the character an uppercase letter? ◦ int ispunct(int) – is the character a punctuation character, i.e., not a space, a letter, or a digit? ◦ int isspace(int) – is the character a whitespace character, e.g., a space, a newline, or a tab?

String Conversions (1) Conversions between numeric types and strings can be necessary in a program. Let’s say you’re reading from a file using getc, and you built a string like " " at some point and you want to convert it to a double, perhaps because you’re going to use it in a calculation. How do you do it? ◦ Realize fscanf does this all the time for you: it reads a bunch of characters from a file and converts them into a numeric type when you ask for it. ◦ How about fprintf? It takes a numeric value and converts it into a bunch of characters to be able to write them to a file.

String Conversions (2) Realize that, we cannot use fprintf and fscanf for our goal since they read from/write into files. However, we need functions with equivalent conversion capability that read from/write into string variables. Fortunately, C provides functions analogs to printf and scanf for doing just these kinds of conversions.

C. Hundhausen, A. O’Fallon10 String Conversions (3) sprintf is completely analogous to printf, except that it writes to a string variable, instead of standard output Example: int course_num = 121, year = 2011; char s[25]; sprintf(s,"CptS %d, Spring %d", course_num, year); s now contains "CptS 121, Spring 2011"

C. Hundhausen, A. O’Fallon11 String Conversions (3) sscanf is completely analagous to scanf, except that it reads from a string variable, rather than from standard input Example: char s[25] = "CptS 121, Spring 2011", dept_code[10], comma, space, semester[10]; int course_num, year; sscanf(s,"%s%d%c%c%s%d",dept_code, &course_num, &comma, &space, semester, &year); dept_code now contains "CptS\0" course_num now contains 121 comma now contains ',' space now contains ' ' semester now contains "Spring\0" year now contains 2011

Arrays of Pointers (1) Remember, last lecture’s assignment: try to implement selection sort to sort a string array This figure illustrates an instance of swapping involved in the algorithm, which requires lots of copying of characters from one memory cell to another. What if we need the list in original order in addition to the sorted list? More copying?

Arrays of Pointers (2) Recall that, in C, when we pass an array into a function, we’re actually passing the address of its first element. Character strings are nothing but an array of chars. So, an array of character strings is nothing but an array of addresses of the first character of each string. We can keep multiple arrays of addresses and when we traverse them, they may give different orders of the original list!

Arrays of Pointers (3) #include #define NUM_NAMES 3 #define MAX_NAME_LENGTH 10 void selection_sort(char *values[], int num_values); int find_smallest(char *values[], int low, int high); int main (void) { char a[NUM_NAMES][MAX_NAME_LENGTH] = {"li", "varun", "gening"}; char *a_ptr [NUM_NAMES]; int i; for( i=0; i < NUM_NAMES; i++) //initializing pointer array a_ptr[i] = a[i]; // or &(a[i][0]) selection_sort(a_ptr, NUM_NAMES); for( i=0; i < NUM_NAMES; i++) //traversing & printing sorted printf("%s\n", a_ptr[i]); return 0; } Notice that a must allocate space for each string, whereas aptr only needs space for pointers to (addresses of) strings

Arrays of Pointers (4) void selection_sort(char *values[], int num_values) { int marker, index_of_smallest; char *temp; for (marker = 0; marker < num_values - 1; marker++) { /* Find the index of the smallest element in unsorted list*/ index_of_smallest = find_smallest(values,marker,num_values-1); /* Swap the smallest value in the subarray i+1.. num_values - 1 with the value[i], thereby putting into place of the ith element. */ temp = values[marker]; values[marker] = values[index_of_smallest]; values[index_of_smallest] = temp; } int find_smallest(char *values[], int low, int high) { int smallest_index, i; smallest_index = low; for (i = low + 1; i <= high; i++) if (strcmp(values[i], values[smallest_index]) < 0) smallest_index = i; return smallest_index; }

C. Hundhausen, A. O’Fallon16 Side note It is important to note that we cannot rely on strcmp to reliably compare character strings with mixed case This is because, in ASCII, all uppercase character come before all lowercase characters If we need to compare mixed-case strings, we need to write our own custom string handling function One approach to writing such a function would be to convert all characters to uppercase before the comparison is made

17 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., ry/