1 / 15

This presentation includes custom animations.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode.

ocean
Télécharger la présentation

This presentation includes custom animations.

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. This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

  2. Strings This slide show describes how C implements strings as arrays of chars. Vocabulary: \0 null Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  3. A string is a data structure that holds a grouping of characters. Pg 434: ... C implements the string data structure using arrays of type char. char MyText[10]; strcpy(MyText, "Strings"); S t r i n g s \0 MyText [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

  4. The array of chars can be compared to the beads on a string. • The total reserved space is like the total length of the string. • The characters are like the beads on the string. • The null is like the knot that ends the string. S t r i n g s \0 MyText [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

  5. Each char requires one byte of storage. The string must be at least long enough to hold each char AND the null. G o B o b c a t s ! \0 StringA [0] [11] [12] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] O h i o \0 StringB [0] [1] [2] [3] [4] [5] H i , J o e \0 StringC [0] [1] [2] [3] [4] [5] [6] [7] c a r r o t \0 StringD [0] [1] [2] [3] [4] [5] [6] [7] [8]

  6. TEST Parts of every loop: Example: Count the number of 'a's in a string. INIT BLOCK AFFECT CountAs A while Text[LetterNum] != '\0’ F DISPLAY Counta GET Text[] End T IF Text[LetterNum] == 'a' Counta = 0 T ++Counta F LetterNum = 0 ++LetterNum A

  7. TEST Parts of every loop: Example: Count the number of lines in a file. INIT BLOCK AFFECT CountLines A while NOT EOF(FileIn) F DISPLAY Lines OPEN FileIn T Lines = 0 ++Lines CLOSE FileIn READ A LINE READ A LINE End A

  8. C includes several functions to input strings and chars. Given the declarations: char Text[80]; FILE *FileIn; Get a string from the keyboard. The string DOES NOT include white space. GET Text[] scanf("\n%s", Text); GET Text[] Get a string from the keyboard. The string CAN include white space. fgets(Text, 80, stdin); READ A LINE GET Text[] fgets(Text, 80, FileIn); Get a string from the file FileIn. fscanf(FileIn, "\n%s", Text); READ A LINE

  9. C includes several functions to input strings and chars. Given the declarations: char Text[80]; FILE *FileIn; scanf("\n%c", &Text[3]); Text[3] = getchar(); GET Text[3] Get a character from the keyboard. Text[3] = getch(); Text[3] = getche(); READ Text[3] fscanf(FileIn,"\n%c", &Text[3]); Get a character from the file, FileIn.

  10. It is important to recognize these functions in <string.h> that work with strings. They are described in Table 9.1 on Page 441. Contents at start of EACH statement S1 S2 I n \0 A t \0 strcpy strcpy(S1,S2); A t \0 A t \0 strncpy strncpy(S1, S2, 1); A n \0 A t \0 strcat strcat(S1, S2); I n A t \0 A t \0 strncat strncat(S1, S2, 1); I n A \0 A t \0 strcmp(S1, S2); strcmp I n \0 A t \0 (strcmp(S1, S2) == 0 when S1 and S2 are the SAME strncmp strncmp(S1, S2, 1); I n \0 A t \0 (strncmp(S1, S2, 2) == 0 when the first 2 chars in S1 and S2 are the SAME 2 strlen strlen(S1); I n \0 A t \0

  11. strcmp is a built-in function that compares two strings. Word1 - Word2 strcmp(Word1, Word2) abc - xyz strcmp("abc", "xyz") < 0 xyz - abc strcmp("xyz", "abc") > 0 abc - abc strcmp("abc", "abc") 0

  12. strcmp is often used in a condition test to check for the entry of a flag value. Enter "Done" for the student name when there are no more students. scanf("\n%s", StudentName); while (strcmp(StudentName, "Done") != 0) { ProcessStudent(); scanf("\n%s", StudentName); } Enter "Green" for the color to indicate the end of the list. scanf("\n%s", Color); while (strcmp(Color, "Green") != 0) { ProcessColor(); scanf("\n%s", Color); }

  13. What is the symbol for NULL? \0 1 byte How much space does NULL require? What distinguishes a string from other arrays of chars? The presence of the NULL at the end of the significant chars. What is the minimum size of an array to hold the word, trees 6 bytes: char Plant[6]; T r e e s \0 In the string above, what is the position of the null? The null is in Plant[5]

  14. In which header file are the str?????? functions found? string.h #include <string.h> How would you store the phrase "Annual Sales" in Heading[]? strcpy(Heading, "Annual Sales"); Which of the str???? functions change the contents of the FIRST argument? strcpy, strncpy, strcat, strncat none Which of the str???? functions change the contents of the SECOND argument? %s In the scanf and printf functions, what is the placeholder for a string? %c In the scanf and printf functions, what is the placeholder for a char? In the scanf function, what is distinctive when the input variable is a string? No ampersands are used in front of the variable name. scanf("\n%s %f", StudentName, &gpa);

More Related