Presentation is loading. Please wait.

Presentation is loading. Please wait.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Similar presentations


Presentation on theme: "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."— 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. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Vocabulary: \0 null

3

4 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]; MyText [0] [1][2][3][4][5][6][7][8][9] strcpy(MyText, "Strings"); Strings\0

5 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. [1][2][3][4][5][6][7][8][9] MyText [0] Strings \0

6 StringA [0] [1][2][3][4][5][6][7][8][9] Each char requires one byte of storage. The string must be at least long enough to hold each char AND the null. StringB [0] [1][2][3][4][5] StringC [0] [1][2][3][4][5][6][7] StringD [0] [1][2][3][4][5][6][7][8] [10] [11][12] GoBobcats!\0 Ohio Hi, Joe carrot

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

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

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

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

11 Contents at start of EACH statement It is important to recognize these functions in that work with strings. They are described in Table 9.1 on Page 441. strcpy strncpy strcat strncat strcmp strncmp strlen In\0At S1S2 strcpy(S1,S2); strncpy(S1, S2, 1); strcat(S1, S2); strncat(S1, S2, 1); strcmp(S1, S2); strncmp(S1, S2, 1); strlen(S1); At\0At An At I nAtAt InA At In At In At In At (strcmp(S1, S2) == 0 when S1 and S2 are the SAME (strncmp(S1, S2, 2) == 0 when the first 2 chars in S1 and S2 are the SAME 2

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

13 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); }

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

15 In the scanf and printf functions, what is the placeholder for a string? %s 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 Which of the str???? functions change the contents of the SECOND argument? none In the scanf and printf functions, what is the placeholder for a char? %c 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); In which header file are the str?????? functions found? string.h#include


Download ppt "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."

Similar presentations


Ads by Google