Advertisement
horselurrver

Reading characters

Aug 2nd, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. void reads(void);
  5.  
  6. FILE * fp;
  7. int main(void){
  8.  
  9.     fp=fopen("/Users/amywang/Dropbox/Code/guessinggame/bob.txt","r");
  10.     if (fp==NULL){
  11.         printf("Whoops, something went wrong.\n");
  12.         exit(1);
  13.     }
  14.     reads();
  15.  
  16.     fclose(fp);
  17.     return 0;
  18. }
  19.  
  20. void reads(void){
  21.     char ch;
  22.     while ((ch=getc(fp))!=EOF){
  23.         if (isalpha(ch)){//it's a letter
  24.             if(isupper(ch)){//uppercase letter
  25.                 printf("%c is a letter, and its numerical position is %d.\n", ch, ch-64);
  26.             }
  27.             else if (islower(ch)) {//lowercase letter
  28.                 printf("%c is a letter, and its numerical position is %d.\n", ch, ch-96);
  29.             }
  30.         }
  31.         else{
  32.             printf("%d\n", -1);
  33.         }
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement