1. Introduction Lecture 11 Lecture 11 Strings 2. B inary R epresentation 3. H ardw are and S oftw are 4. H igh Level Languages • We have already come across strings 5. S tandard input and output e.g. puts(“Hello”); 6. Operators, expression and statem ents • Here “Hello” is a literal string or string constant 7. M aking Decisions • We can also have string variables 8. Looping 9. A rrays • In C strings are not a simple data type 10. B asics of pointers • A string is a null-terminated array of char 11. S trings • This means that the end of a string is marked by 12. B asics of functions a “sentinel” character, ‘\0’ (ASCII code=0) 13. M ore about functions 14. Files h e l l o \0 ? 14. D ata S tructures 16. C ase study: lottery num ber generator My first name is David. My first name is David. Declaring String My last name is Hamill. My last name is Hamill. myname1[0] = 'D' (ASCII 68) myname1[0] = 'D' (ASCII 68) Declaring String Variables myname1[1] = 'a' (ASCII 97) myname1[1] = 'a' (ASCII 97) myname1[2] = 'v' (ASCII 118) Variables myname1[2] = 'v' (ASCII 118) myname1[3] = 'i' (ASCII 105) myname1[3] = 'i' (ASCII 105) myname1[4] = 'd' (ASCII 100) myname1[4] = 'd' (ASCII 100) myname1[5] = ' ' (ASCII 0) myname1[5] = ' ' (ASCII 0) myname2[0] = 'H' (ASCII 72) • Simplest myname2[0] = 'H' (ASCII 72) myname2[1] = 'a' (ASCII 97) myname2[1] = 'a' (ASCII 97) • Because the ‘\0’ character marks the myname2[2] = 'm' (ASCII 109) myname2[2] = 'm' (ASCII 109) myname2[3] = 'i' (ASCII 105) myname2[3] = 'i' (ASCII 105) char message[6]; myname2[4] = 'l' (ASCII 108) end of a string it is ok to have more myname2[4] = 'l' (ASCII 108) myname2[5] = 'l' (ASCII 108) myname2[5] = 'l' (ASCII 108) myname2[6] = ' ' (ASCII 0) • Initialising myname2[6] = ' ' (ASCII 0) space than needed /* Example: strings as null-terminated arrays of char */ /* Example: strings as null-terminated arrays of char */ char message[6]={‘H’,’e’,’l’,’l’,’o’,\o’}; char message[80]=“Hello”; #include <stdio.h> strings1.c #include <stdio.h> strings1.c • But this is so common that C provides main() • The 74 bytes beyond main() { { char myname1[6] = "David"; /* 5 characters + '\0' */ char myname1[6] = "David"; /* 5 characters + '\0' */ the shorthand char myname2[] = "Hamill"; /* size set automatically */ the ‘\0’ contain rubbish char myname2[] = "Hamill"; /* size set automatically */ int i; int i; at this point but are not printf("My first name is %s.\n", myname1); char message[6]=“Hello”; or printf("My first name is %s.\n", myname1); printf("My last name is %s.\n\n", myname2); printf("My last name is %s.\n\n", myname2); char message[]=“Hello”; printed for (i = 0; i <= 5; i++) for (i = 0; i <= 5; i++) printf("myname1[%i] = '%c' (ASCII %i)\n", i, myname1[i], printf("myname1[%i] = '%c' (ASCII %i)\n", i, myname1[i], • Which allocates the required 6 bytes myname1[i]); myname1[i]); putchar('\n'); putchar('\n'); for (i = 0; i <= 6; i++) automatically for (i = 0; i <= 6; i++) printf("myname2[%i] = '%c' (ASCII %i)\n", i, myname2[i], printf("myname2[%i] = '%c' (ASCII %i)\n", i, myname2[i], myname2[i]); myname2[i]); putchar('\n'); putchar('\n'); } } Declaring String Variables Declaring String Variables • As with all arrays, overrun must be avoided • The scanf function allows us to input a string, • This is particularly easy with string specifying the maximum number of characters manipulation /* Example: inputting strings from keyboard with scanf */ /* Example: inputting strings from keyboard with scanf */ /* Example: inputting strings from keyboard with scanf */ /* Example: inputting strings from keyboard with scanf */ #include <stdio.h> strings3.c #include <stdio.h> strings3.c #include <stdio.h> strings2.c #include <stdio.h> strings2.c main() main() main() main() { { { { char word[11]; /* a string, up to 10 characters (+ '\0') */ char word[11]; /* a string, up to 10 characters (+ '\0') */ char word[11]; /* a string, up to 10 characters (+ '\0') */ char word[11]; /* a string, up to 10 characters (+ '\0') */ char sentence[] = "Very interesting."; char sentence[] = "Very interesting."; char sentence[] = "Very interesting."; char sentence[] = "Very interesting."; /* The simplest approach. What might happen if a long word is /* A better approach. What happens if a long word is /* The simplest approach. What might happen if a long word is /* A better approach. What happens if a long word is entered? Why? */ entered? Why? */ entered? Why? */ entered? Why? */ printf("Enter a word, not more than 10 characters: "); printf("Enter a word, not more than 10 characters: "); printf("Enter a word, not more than 10 characters: "); scanf("%s", word); printf("Enter a word, not more than 10 characters: "); scanf("%s", word); scanf("%10s", word); scanf("%10s", word); printf("You entered \"%s\". %s\n\n", word, sentence); printf("You entered \"%s\". %s\n\n", word, sentence); printf("You entered \"%s\". %s\n\n", word, sentence); printf("You entered \"%s\". %s\n\n", word, sentence); } } } } 1
Recommend
More recommend