1 / 17

Strings (II) H&K Chapter 9

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

Download Presentation

Strings (II) H&K Chapter 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings (II)H&K Chapter 9 Instructor – GokcenCilingir 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 <ctype.h> 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');

  7. Character Operations (6) • <ctype.h> 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?

  8. 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 "3.14159" 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.

  9. 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.

  10. 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"

  11. 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_codenow contains "CptS\0"course_numnow contains 121comma now contains ','space now contains ' ' semester now contains "Spring\0"year now contains 2011

  12. 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?

  13. 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!

  14. Arrays of Pointers (3) #include <stdio.h> #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

  15. 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; }

  16. 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 (6th Ed.), Addison-Wesley, 2010 • P.J. Deitel & H.M. Deitel, C How to Program (5th Ed.), Pearson Education , Inc., 2007. • http://www.cplusplus.com/reference/clibrary/

More Related