Download presentation
Presentation is loading. Please wait.
Published byDorcas Cook Modified over 9 years ago
1
Chapter 10 - Character Strings
2
Array of Characters char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’ }; word[0] 'H' word[1] 'e' word[2] 'l' word[3] 'l' word[4] 'o' word[5] '!'
3
Program 10.1 /* * Function to concatenate two character strings */ #include concat (char result[], char str1[], int n1, char str2[], int n2) { int i; // copy str1 to result for (i = 0; i < n1; i++) result[i] = str1[i]; for (i = 0; i < n2; i++) result[n1 + i] = str2[i]; }
4
Program 10.1 (continued) main() { char s1[5] = {'T', 'e', 's', 't', ' '}; char s2[6] = {'w', 'o', 'r', 'k', 's', '.'}; char s3[11]; int i; concat (s3, s1, 5, s2, 6); for (i = 0; i < 11; i++) printf("%c", s3[i]); printf ("\n"); system ("PAUSE"); }
5
Program 10.1 Output
6
Variable Length Character Strings word[0] 'H' word[1] 'e' word[2] 'l' word[3] 'l' word[4] 'o' word[5] '!' word[6] '\0' char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’ }; NULL Character Terminates a String
7
Program 10.2 /* * Function to count the number of characters in a string */ #include int string_length (char string[]) { int count = 0; while(string[count] != '\0') count++; return (count); }
8
Program 10.2 (continued) main() { char word1[] = {'a', 's', 't', 'e', 'r', '\0'}; char word2[] = {'a', 't', '\0'}; char word3[] = {'a', 'w', 'e', '\0'}; int i; printf ("%i %i %i\n", string_length (word1), string_length (word2), string_length (word3)); system ("PAUSE"); }
9
Program 10.2 Output
10
Initializing and Displaying Character Strings char word[] = “Hello!”; char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’ }; char word[7] = “Hello!”; char word[6] = “Hello!”; These Statements Are Equivalent Leave Room for the NULL Character
11
Program 10.3 /* * Function to concatenate two character strings */ #include concat (char result[], char str1[], char str2[]) { int i; int j; // copy str1 to result for (i = 0; str1[i] != '\0'; i++) result[i] = str1[i]; for (j = 0; str2[j] != '\0'; j++) result[i + j] = str2[j]; result[i+j] = '\0'; }
12
Program 10.3 (continued) main() { char s1[] = "Test "; char s2[] = "works."; char s3[20]; concat (s3, s1, s2); printf("%s\n", s3); system ("PAUSE"); }
13
Program 10.3 Output
14
Testing Two Character Strings for Equality Since the C Programming Language does not support a data type of string we cannot directly test two strings to see if they are equal with a statement such as if ( string1 == string2 )
16
Program 10.4 /* * Function to determine if two strings are equal */ #include #define TRUE 1 #define FALSE 0 int equal_strings (char s1[], char s2[]) { int i = 0; while ((s1[i] == s2[i]) && (s1[i] != '\0') && (s2[i] != '\0')) i++; if ((s1[i] == '\0') && (s2[i] == '\0')) return(TRUE); else return(FALSE); }
17
Program 10.4 (continued) main() { char stra[] = "string compare test"; char strb[] = "string"; printf ("%i\n", equal_strings (stra, strb)); printf ("%i\n", equal_strings (stra, stra)); printf ("%i\n", equal_strings (strb, "string")); system ("PAUSE"); }
18
Program 10.4 Output
19
Inputting Character Strings char string[81]; scanf ( “%s”, string); char s1[81], s2[81], s3[81]; scanf ( “%s%s%s”, s1, s2, s3);
20
Program 10.5 /* * Program to illustrate the %s scanf format characters */ #include main() { char s1[81]; char s2[81]; char s3[81]; printf ("Enter text:\n"); scanf ("%s%s%s", s1, s2, s3); printf ("\ns1 = %s\ns2 = %s\ns3 = %s\n", s1, s2, s3); system ("PAUSE"); }
21
Program 10.5 Output
22
Program 10.6 /* * Function to read a line of text from a terminal */ #include read_line (char buffer[]) { char character; int i = 0; do { character = getchar(); buffer[i] = character; i++; } while (character != '\n'); buffer[i-1] = '\0'; }
23
Program 10.6 (continued) main() { char line[81]; int i; for (i=0; i < 3; i++) { read_line(line); printf ("%s\n\n", line); } system ("PAUSE"); }
24
Program 10.6 Output
25
Program 10.7 #include #define TRUE 1 #define FALSE 0 /* * Function to determine if a character is alphabetic */ alphabetic (char c) { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) return (TRUE); else return (FALSE); }
26
Program 10.7 (continued) /* * Function to count the number of words in a string */ count_words (char string[]) { int looking_for_word = TRUE; int word_count = 0; int i; for (i=0; string[i] != '\0'; i++) { if (alphabetic(string[i])) { if (looking_for_word) { word_count++; looking_for_word = FALSE; } else looking_for_word = TRUE; } return (word_count); }
27
Program 10.7 (continued) main() { char text1[] = "Well, here goes."; char text2[] = "And here we go... again."; printf ("%s - words = %i\n", text1, count_words (text1)); printf ("%s - words = %i\n", text2, count_words (text2)); system ("PAUSE"); }
28
Program 10.7 Output
29
Execution of count_words Function istring[i]word_countlooking_for_word 01 0 'W'10 1 'e'10 2 'l'10 3 10 4 ','11 5 ‘ '11 6 'h'20 7 'e'20 8 'r'20 9 'e'20 10 ‘ '21 11 'g'30 12 'o'30 13 'e'30 14 's'30 15 '.'31 16 '\0'31
30
The NULL String A Character String that contains no characters other than the NULL Character has a special name in the C Programming Language, it is called the NULL String. The string length will correctly return 0. char buffer[100] = “”;
31
Program 10.8 #include #define TRUE 1 #define FALSE 0 /***** Insert alphabetic function here *****/ /***** Insert read_line function here *****/ /***** Insert count_words function here *****/
32
Program 10.8 (continued) main() { char text[81]; int end_of_text = 0; int total_words = 0; printf ("Type in your text.\n"); printf ("When you are done, press 'RETURN'.\n\n"); while (!end_of_text) { read_line (text); if (text[0] == '\0') end_of_text = TRUE; else total_words += count_words (text); } printf("\nThere are %i words in the above text.\n", total_words); system ("PAUSE"); }
33
Program 10.8 Output
34
Escape Characters Escape CharacterName \aaudible alert \bbackspace \fform feed \nnewline \rcarriage return \thorizontal tab \vvertical tab \\backslash \”double quote \'single quote \?question mark \nnnoctal character value nnn \xnnhexadecimal character value nn
35
More on Constant Strings If you put a backslash character at the very end of the line and followed it immediately by a carriage return, it will tell the C Compiler to ignore the end of line. This line continuation technique is used primarily for continuing long constant character strings. char letters[] = “abcdefghijklmnopqrstuvwxyz\ ABCDEFGHIJKLMNOPQRSTUVWXYZ”; An even easier way of breaking up long character strings is to divide them into two or more adjacent strings. char letters[] = “abcdefghijklmnopqrstuvwxyz” “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
36
Character Strings, Structures, and Arrays Suppose we wanted to write a computer program that acted as a dictionary. One of the first thoughts would be to representation of the word and it definition. Since the word and its definition are logically related, the notion of a structure comes immediately to mind. struct entry { char word[10]; char definition[50]; }; struct entry dictionary[100];
37
Program 10.9 /* * Dictionary lookup program */ #include #define TRUE 1 #define FALSE 0 struct entry { char word[10]; char definition[50]; }; /***** Insert equal_string function here *****/
38
Program 10.9 /* * Function to lookup a word inside a dictionary */ int lookup (struct entry dictionary[], char search [], int entries) { int i; for (i=0; i < entries; i++) if (equal_strings (search, dictionary[i].word)) return (i); return (-1); }
39
Program 10.9 main() { struct entry dictionary[100] = { { "aardvark", "a burrowing African mammal" }, { "abyss", "a bottomless pit" }, { "acumen", "mentally sharp; keen" }, { "addle", "to become confused" }, { "aerie", "a high nest" }, { "affix", "to append; attach" }, { "agar", "a jelly made from seaweed" }, { "ahoy", "a nautical call of greeting" }, { "aigrette", "an ornamental cluster of feathers" }, { "ajar", "partially opened" } }; char word[10]; int entries = 10; int entry_number;
40
Program 10.9 printf("Enter word: "); scanf("%9s", word); entry_number = lookup (dictionary, word, entries); if (entry_number != -1) printf ("%s\n", dictionary[entry_number].definition); else printf ("Sorry, that word is not in my dictionary.\n"); system ("PAUSE"); }
41
Program 10.9 Output
42
A Better Search Method Step 1: Set low to 0, high to n – 1 Step2: If low > high, x does not exist in M and the algorithm terminates Step 3: Set mid to (low + high) / 2 Step 4: if M[mid] < x, set low to mid +1 and go to Step 2 Step 5: if M[mid] > x, set high to mid -1 and go to Step 2 Step 6: M[mid] equals x and the algorithm terminates Binary Search Algorithm
43
/* * Binary Search Algorithm */ int lookup (struct entry M[], char x[], int n) { int low = 0; /* Step 1: */ int high = n - 1; /* Step 1: */ int mid; int result; while (low <= high) { mid = (low + high) / 2; /* Step 3: */ result = compare_strings (M[mid].word, x); if (result == -1) low = mid + 1; /* Step 4: */ else if (result == 1) high = mid - 1; /* Step 5: */ else return (mid); /* Step 6: */ } return (-1); /* Step 2: */ }
44
Examples of Binary Search
47
Program 10.10 /* * Dictionary lookup program */ #include struct entry { char word[10]; char definition[50]; };
48
Program 10.10 (continued) /* * Function to compare two character strings */ int compare_strings (char s1[], char s2[]) { int i = 0; while ((s1[i] == s2[i]) && (s1[i] != '\0') && (s2[i] != '\0')) i++; if (s1[i] < s2[i] ) /* s1 < s2 */ return(-1); else if (s1[i] == s2[i]) /* s1 == s2 */ return (0); else /* s1 > s2 */ return(+1); }
49
Program 10.10 (continued) /* * Function to lookup a word inside a dictionary */ int lookup (struct entry dictionary[], char search [], int entries) { int low = 0; int high = entries - 1; int mid; int result; while (low <= high) { mid = (low + high) / 2; result = compare_strings (dictionary[mid].word, search); if (result == -1) low = mid + 1; else if (result == 1) high = mid - 1; else return (mid); /* found it */ } return (-1); /* not found */ }
50
Program 10.10 (continued) main() { struct entry dictionary[100] = { { "aardvark", "a burrowing African mammal" }, { "abyss", "a bottomless pit" }, { "acumen", "mentally sharp; keen" }, { "addle", "to become confused" }, { "aerie", "a high nest" }, { "affix", "to append; attach" }, { "agar", "a jelly made from seaweed" }, { "ahoy", "a nautical call of greeting" }, { "aigrette", "an ornamental cluster of feathers" }, { "ajar", "partially opened" } }; char word[10]; int entries = 10; int entry_number;
51
Program 10.10 (continued) printf("Enter word: "); scanf("%9s", word); entry_number = lookup (dictionary, word, entries); if (entry_number != -1) printf ("%s\n", dictionary[entry_number].definition); else printf ("Sorry, that word is not in my dictionary.\n"); system ("PAUSE"); }
52
Program 10.10 Output
53
Program 10.10 Output (Rerun)
54
ASCII Character Table 0 0000000 NUL(Null char.) 1 1010000 0001SOH(Start of Header) 2 2020000 0010STX(Start of Text) 3 3030000 0011ETX(End of Text) 4 4040000 0100EOT(End of Transmission) 5 5050000 0101ENQ(Enquiry) 6 6060000 0110ACK(Acknowledgment) 7 7070000 0111BEL(Bell) 8 10080000 1000BS(Backspace) 9 11090000 1001HT(Horizontal Tab) 10 120A0000 1010LF(Line Feed) 11 130B0000 1011VT(Vertical Tab) 12 140C0000 1100FF(Form Feed) 13 150D0000 1101CR(Carriage Return) 14 160E0000 1110SO(Shift Out) 15 170F0000 1111SI(Shift In) 16 20100001 0000DLE(Data Link Escape) 17 21110001 DC1(XON) (Device Control 1) 18 22120001 0010DC2(Device Control 2) 19 23130001 0011DC3(XOFF)(Device Control 3) 20 24140001 0100DC4(Device Control 4) 21 25150001 0101NAK(Negativ Acknowledgemnt) 22 26160001 0110SYN(Synchronous Idle) 23 27170001 0111ETB(End of Trans. Block) 24 30180001 1000CAN(Cancel) 25 31190001 1001EM(End of Medium) 26 321A0001 1010SUB(Substitute) 27 331B0001 1011ESC(Escape) 28 341C0001 1100FS(File Separator) 29 351D0001 1101GS(Group Separator) 30 361E0001 1110RS(Reqst to Send)(Rec. Sep.) 31 371F0001 1111US(Unit Separator) 32 40200010 0000SP(Space) 33 41210010 0001!(exclamation mark) 34 42220010 "(double quote) 35 43230010 0011#(number sign) 36 44240010 0100$(dollar sign) 37 45250010 0101%(percent) 38 46260010 0110&(ampersand) 39 47270010 0111'(single quote) 40 50280010 1000((left/open parenthesis) 41 51290010 1001)(right/closing parenth.) 42 522A0010 1010*(asterisk) 43 532B0010 1011+(plus) 44 542C0010 1100,(comma) 45 552D0010 1101-(minus or dash) 46 562E0010 1110.(dot) 47 572F0010 1111/(forward slash) 48 60300011 00000 49 61310011 00011 50 62320011 00102 51 63330011 3 52 64340011 01004 53 65350011 01015 54 66360011 01106 55 67370011 01117 56 70380011 10008 57 71390011 10019 58 723A0011 1010:(colon) 59 733B0011 1011;(semi-colon) 60 743C0011 1100<(less than) 61 753D0011 1101=(equal sign) 62 763E0011 1110>(greater than) 63 773F0011 1111?(question mark) 64 100400100 0000@(AT symbol) 65 101410100 0001A 66 102420100 0010B 67 103430100 0011C 68 104440100 D 69 105450100 0101E 70 106460100 0110F 71 107470100 0111G 72 110480100 1000H 73 111490100 1001I 74 1124A0100 1010J 75 1134B0100 1011K 76 1144C0100 1100L 77 1154D0100 1101M 78 1164E0100 1110N 79 1174F0100 1111O 80 120500101 0000P 81 121510101 0001Q 82 122520101 0010R 83 123530101 0011S 84 124540101 0100T 85 125550101 U 86 126560101 0110V 87 127570101 0111W 88 130580101 1000X 89 131590101 1001Y 90 1325A0101 1010Z 91 1335B0101 1011[(left/opening bracket) 92 1345C0101 1100\(back slash) 93 1355D0101 1101](right/closing bracket) 94 1365E0101 1110^(caret/circumflex) 95 1375F0101 1111_(underscore) 96 140600110 0000` 97 141610110 0001a 98 142620110 0010b 99 143630110 0011c 100 144640110 0100d 101 145650110 0101e 102 146660110 f 103 147670110 0111g 104 150680110 1000h 105 151690110 1001i 106 1526A0110 1010j 107 1536B0110 1011k 108 1546C0110 1100l 109 1556D0110 1101m 110 1566E0110 1110n 111 1576F0110 1111o 112 160700111 0000p 113 161710111 0001q 114 162720111 0010r 115 163730111 0011s 116 164740111 0100t 117 165750111 0101u 118 166760111 0110v 119 167770111 w 120 170780111 1000x 121 171790111 1001y 122 1727A0111 1010z 123 1737B0111 1011{(left/opening brace) 124 1747C0111 1100|(vertical bar) 125 1757D0111 1101}(right/closing brace) 126 1767E0111 1110~(tilde) 127 1777F0111 1111DEL(delete)
55
ASCII Alphabetic Characters 65 101410100 0001A 66 102420100 0010B 67 103430100 0011C 68 104440100 D 69 105450100 0101E 70 106460100 0110F 71 107470100 0111G 72 110480100 1000H 73 111490100 1001I 74 1124A0100 1010J 75 1134B0100 1011K 76 1144C0100 1100L 77 1154D0100 1101M 78 1164E0100 1110N 79 1174F0100 1111O 80 120500101 0000P 81 121510101 0001Q 82 122520101 0010R 83 123530101 0011S 84 124540101 0100T 85 125550101 U 86 126560101 0110V 87 127570101 0111W 88 130580101 1000X 89 131590101 1001Y 90 1325A0101 1010Z 97 141610110 0001a 98 142620110 0010b 99 143630110 0011c 100 144640110 0100d 101 145650110 0101e 102 146660110 f 103 147670110 0111g 104 150680110 1000h 105 151690110 1001i 106 1526A0110 1010j 107 1536B0110 1011k 108 1546C0110 1100l 109 1556D0110 1101m 110 1566E0110 1110n 111 1576F0110 1111o 112 160700111 0000p 113 161710111 0001q 114 162720111 0010r 115 163730111 0011s 116 164740111 0100t 117 165750111 0101u 118 166760111 0110v 119 167770111 w 120 170780111 1000x 121 171790111 1001y 122 1727A0111 1010z
56
Character Operations Whenever a character constant or variable is used in an expression, it is automatically converted to, and subsequently treated as, an integer value. For example, (‘a’ <= c) && (c <= ‘z’) (97 <= c) && (c <= 122)
57
ASCII Numeric Characters 48 60300011 00000 49 61310011 00011 50 62320011 00102 51 63330011 3 52 64340011 01004 53 65350011 01015 54 66360011 01106 55 67370011 01117 56 70380011 10008 57 71390011 10019
58
Character Operations Suppose the character variable c contained one of the characters ‘0’ thru ‘9’ and the we wished to convert this value into the corresponding integer 0 thru 9. Since the digits of virtually all character sets are represented by sequential integer values, we can easily convert c into its integer equivalent by subtracting the character constant ‘0’ from it. For example, char c = ‘5’; int i = c – ‘0’; c = 53; i = 53 – 48;
59
Program 10.11 /* * Function to convert a string to an integer */ #include int string_to_integer (char string[]) { int integer_value; int result = 0; int i = 0; for (i=0; ('0' <= string[i]) && (string[i] <= '9'); i++) { integer_value = string[i] - '0'; result = result *10 + integer_value; } return(result); }
60
Program 10.11 (continued) main() { printf ("%i\n", string_to_integer ("245")); printf ("%i\n", string_to_integer ("100") + 25); printf ("%i\n", string_to_integer ("13x5")); system ("PAUSE"); }
61
Program 10.11 Output
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.