1 //程序10.8
  2 
  3 #include <stdio.h>
  4 #include <stdbool.h>
  5  
  6 bool alphabetic (const char c){
  7     if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  8         return true;
  9     else 
 10         return false;
 11 } 
 12 
 13 void readLine (char buffer[]){
 14     char character;
 15     int i = 0;
 16     
 17     do{
 18         character = getchar ();
 19         buffer[i] = character;
 20         ++i;
 21     }while (character != '\n');
 22     
 23     buffer[i - 1] = '\0';
 24 }
 25 
 26 int countWords (const char string[]){
 27     int i, wordCount = 0;
 28     bool lookingForWord = true, alphabetic (const char c);
 29     
 30     for (i = 0;string[i] != '\0'; ++i)
 31         if (alphabetic(string[i])){
 32             if (lookingForWord){
 33                 ++wordCount;
 34                 lookingForWord = false;
 35             }
 36         }
 37     else
 38         lookingForWord = true;
 39     
 40     return wordCount;
 41 }
 42 
 43 int main (void){
 44     char text[81];
 45     int totalWords = 0;
 46     int countWords (const char string[]);
 47     void readLine (char buffer[]);
 48     bool endOfText = false;
 49 
 50     printf ("Type in your text.\n");
 51     printf ("When you are done,press 'RETURN'.\n\n");
 52 
 53     while (! endOfText){
 54         readLine (text);
 55         if (text[0] == '\0')
 56         endOfText = true;
 57         else
 58         totalWords += countWords (text);
 59     }    
 60     printf ("\nThere are %i words in the above text.\n", totalWords);
 61     
 62     return 0;
 63 }
 64 
 65 //程序10.9
 66 #include <stdio.h>
 67 #include <stdbool.h>
 68 
 69 struct entry{
 70     char word[15];
 71     char definition[50];
 72 };
 73 
 74 bool equalStrings (const char s1[], const char s2[]){
 75     int i =0;
 76     bool areEqual;
 77     
 78     while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
 79         ++i;
 80     
 81     if (s1[i] == '\0' && s2[i] == '\0')
 82         areEqual = true;
 83     else
 84         areEqual = false;
 85     return areEqual;
 86 }
 87 
 88 int lookup(const struct entry dictionary[], const char search[],
 89         const int entries){
 90     int i;
 91     bool equalStrings (const char s1[],const char s2[]);
 92 
 93     for (i = 0; i < entries; ++i)
 94         if (equalStrings (search, dictionary[i].word))
 95             return i;
 96     return -1;        
 97 }
 98 
 99 int main(void){
100     const struct entry dictionary[100] = {
101         {"aardvard", "a burrowing African mammal"},
102         {"abyss", "a bottomless pit"},
103         {"acumen", "mentally sharp;keen"},
104         {"addle", "to become confused"},
105         {"aerie", "a high nest"},
106         {"affix", "to append; atach"},
107         {"agar", "a jelly made from seaweed"},
108         {"ahoy","a nautical call of greeting"},
109         {"aigrette", "an ornamental cluster of feathers"},
110         {"ajar", "patially opend"}};
111     char word[10];
112     int entries = 10;
113     int entry;
114     int lookup (const struct entry dictionary[], const char search[],
115         const int entries);
116         
117     printf ("Enter word:");
118     scanf ("%14s", word);
119     entry = lookup (dictionary,word,entries);
120     
121     if (entry != -1)
122         printf ("%s\n", dictionary[entry].definition);
123     else
124         printf ("Sorry,the word %s is not in my dictionary.\n", word);
125     
126     return 0;
127     
128 }
129 
130 //第三题
131 
132 #include <stdio.h>
133 #include <stdbool.h>
134 
135 bool wordchar (const char c){
136     if  ( (c >= 'a'  &&  c <= 'z') || (c >= 'A'  &&  c <= 'Z') || c == '\'')
137         return true;
138     else
139         return false;
140 }
141 
142 bool  numchar (const char c){
143 
144     if ( (c >= '0' && c <= '9') || c == '.' || c == ',' || c == '-' )
145        return  true;
146     else
147        return  false;
148 }
149 
150 // Function to count the number of words in a string 
151 
152 int  countWords (const char string[]){
153     int  i,  wordCount = 0;
154     bool lookingForWord = true, wordchar (const char c), numchar (const char c);
155 
156     for ( i = 0; string[i] != '\0';  ++i )
157         if ( wordchar (string[i])  || numchar (string[i]) ) {
158             if ( lookingForWord ) {
159                  ++wordCount;
160                  lookingForWord = false;
161             }
162         }
163         else
164             lookingForWord = true;
165 
166     return wordCount;
167 }
168 
169 // Here's a sample main routine and associated output:
170 
171 int main (void){
172     char  text1[] = "The sum of $552,227 and $-1,204.50 is $551,002.50";
173     char  text2[] = "It isn't that I don't understand you.";
174     int  countWords (const char  string[]);
175 
176     printf ("%s -- words = %i/n", text1, countWords (text1));
177     printf ("%s -- words = %i/n", text2, countWords (text2));
178 }