Advertisement
Guest User

[C] Proper Capitalization of Sentences

a guest
Nov 18th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5.  
  6. int file_eof(FILE *in)  // could use "int *ch" to avoid seeking
  7. {
  8.    if(!in) return -1;
  9.    int c;
  10.  
  11.    c = fgetc(in);
  12.    if(c == EOF) return 1;
  13.  
  14.    c = fseek(in, -1, SEEK_CUR);
  15.    if(c != 0)
  16.      return -2;
  17.  
  18.    return 0;
  19. }
  20.  
  21.  
  22. int file_eofHandle(FILE *in)
  23. {
  24.    int c = file_eof(in);
  25.  
  26.    if(c < 0)
  27.    {
  28.       printf("[-] file_eof() [%d]\n", c);
  29.       exit(1);
  30.    }
  31.  
  32.    if(c == 1)
  33.      exit(0);  // should use fclose(in), but program termination closes it
  34.  
  35.    return 0;
  36. }
  37.  
  38.  
  39. int main(int argc, char **argv)
  40. {
  41.    FILE *in, *out;
  42.    int c;
  43.    int sBegin = 1;  // beginning of sentence
  44.  
  45.    if(argc < 2)
  46.      return 1;
  47.  
  48.    in = fopen(argv[1], "r");
  49.    if(!in)
  50.      return 2;
  51.  
  52.    out = stdout; // there's no need to write to a file
  53.                  // that's what piping is for
  54.                  //  if using *nix system file descriptor, then stdout is 1
  55.  
  56.    while(1)
  57.    {
  58.       file_eofHandle(in);
  59.  
  60.       c = fgetc(in);
  61.  
  62.       if(c == '.')
  63.         sBegin = 1;
  64.  
  65.       if(isalpha(c) != 0)  // using if((c >= 0xwhatever && c <= 0xwhatever) ||
  66.       {                    //          (blah >= lakjsdflkj...)) is harder to read
  67.                            // and a function should be used anyway
  68.          if(sBegin != 0)
  69.          {
  70.             c = toupper(c);
  71.             fputc(c, out);
  72.             sBegin = 0;
  73.          }
  74.          else
  75.             fputc(c, out);
  76.       }
  77.       else
  78.         fputc(c, out);
  79.    }
  80.  
  81.    return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement