Characters, Arrays of characters, and strings You can read and store an individual character in a type char variable: char let1, let2; printf("Enter your initials - 2 letters:"); scanf(" %c %c", &let1, &let2); printf("Your initials are: %c.%c.\n", let1, let2); Enter your initials - 2 letters: e k Your initials are: e.k. The format descriptor %c preceded by a space causes the white space before a character to be ignored. Without the space, white space would not be ignored. scanf("%c%c", let1, let2); … Enter your initials - 2 letters: e k Your initials are: . . Each character is stored as a binary value in let1 and let2. The values correspond to the ASCII code for characters (American Standard Code for Information Interchange). 8 bits (1 byte) are used to code each character. Examples: space 23 0 48 1 49 … 9 57 A 65 B 66 C 67 … Z 90 a 97 b 98 z 122 To find the code for a paricular character, you can write: int code; code = (int) 'a'; printf("%c %d\n", 'a', code); This should print out: a 97 Some special non-printable control characters: '\n' carriage return, line feed '\t' tab character '\0' null character - terminates a string. let1 = '\n'; code = (int) let1; printf("*%c *%d\n", let1, code); * *10 Certain character functions are available in char toupper(char ch): returns the uppercase form of its argument if ch is a letter; otherwise, returns ch. char toupper(char ch) { if (ch >= 'a' && ch <= 'z') /* test for lowercase char */ return ( (char) ((int) ch - 32)); else return ch; } Other character functions: char tolower(char ch) - returns the lowercase equivalent of a letter - returns all other characters unchanged. int isalpha(char ch) - returns 1 (true) if ch is alphabetic; otherwise, returns 0 (false) int islower(char ch) - returns 1 (true) if ch is lowercase. int isupper(char ch) - returns 1 (true) if ch is uppercase. int isdigit(char ch) - returns 1 (true) if ch is a digit character. We can store a collection of characters in an array: This is a sentence. If we read these one by one, we can store them as individual characters in an array. char line[SIZE], next; int I, line_size; printf("Enter a line of characters ending with return:"); I = 0; scanf("%c", &next); while (I < SIZE && next != '\n') { line[I] = next; I++; scanf("%c", &next); } /* save actual line size */ line_size = I; What would be stored? line[0] line[1] … line[18] line_size T h . 19 You can access any of the individual characters just like you access other data in an array: line[1] is 'h'. Although we are not really worrying about strings in this course, it is possible to declare and use strings of characters in C. char line[SIZE] = "This is a sentence."; directly stores the indicated string in array line. Looks same as before. One difference: line[19] contains the character '\0' which is the null character - C uses it to terminate strings. The statement: printf("The string in line is: %s\n", line); would display: The string in line is: This is a sentence. You could also display this string by using the loop: I = 0; while (line[I] != '\0') { printf("%c", line[I]); I++; } This loop displays: This is a sentence. If you want to terminate this line, you can use: printf("\n"); You can also use scanf to read in a string: printf("Enter a string: "); scanf("%s", line); Stores up to the first white space character in line. So if you type in, Enter a string: This is a sentence. Only the first word "This" would be stored in line filling elements line[0] through line[3]. line[4] would contain the character '\0'. The rest of the sentence would be processed by the next scanf. If you want to store the whole string, you will need to substitute some character for spaces: e.g., "This#is#a#sentence. Arrays of strings: char names[3][12] = {"Billy Joel", "Jane Doe", "Sam Silver"}; names[0] is string "Billy Joel"; names[0][1] is 'B', names[0][2] is 'i', names[0][ ] is 'e', names[0][10] is ' '. Can also read data into this array: for (I = 0; I < 3; I++) { printf("Enter name of student %d without spaces", I); scanf("%s", names[I]); } type in: Billy#Joel type in: Jane#Doe type in: Sam#Silver What does this function do: void mystery(char names[3][]) { int I, j; for (I = 0; I < 3; I++) for (j = 0; j < 12, j++) if (names[I][j] == '#') names[I][j] = ' '; }