Page 119 - Programming Microcontrollers in C
P. 119

104    Chapter 2  Advanced C Topics

                          if(!isalnum(*w=tolower(getch())))
                          {
                              ungetch(*w);
                              break;
                          }
                       *w=‘\0’;
                       return word[0];
                   }
                              The first executable statement

                   while(isspace(c=tolower(getch())));
                          includes two standard C functions. The function isspace() has
                          its prototype in the header file ctype.h. This function returns a
                          TRUE if its argument is a space and FALSE otherwise. The second
                          function, tolower(), is also prototyped in ctype.h. It tests
                          the argument and returns a lower case letter if the argument is upper
                          case. Therefore, this operation will loop until it receives a nonspace
                          input, and the lower case version of the letter input will be stored in c.
                              If c is not an EOF, it is put into the next open location of the word
                          array and the pointer into this array is incremented. If the return is an
                          EOF, the second if statement will execute. The if statement

                   if(!isalpha(c))
                   {
                       *w=‘\0’;
                       return c;
                   }

                          tests to determine if the character from the input stream is a letter.
                          isalpha() returns a TRUE if its argument is a letter and a FALSE
                          otherwise. If the character taken from the input stream is an EOF,
                          isalpha() will return a FALSE and the statement following the
                          if will be executed. In this case, a zero character is written to the
                          word, and the EOF is returned to the calling function.
                              If the return is a letter, the following sequence will be executed:

                   for( ; —lim >0 ; w++)
                       if(!isalnum(*w=tolower(getch())))
                       {
                          ungetch(*w);
   114   115   116   117   118   119   120   121   122   123   124