Advertisement
Guest User

Proper Capitalization

a guest
Nov 18th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 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. void 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;
  36. }
  37.  
  38.  
  39. int file_previousChar(FILE *in, int *pChar)
  40. {
  41. if(in == NULL || pChar == NULL)
  42. return -1;
  43.  
  44. long fPos;
  45.  
  46. fPos = ftell(in);
  47. if(fPos < 2)
  48. { *pChar = 0;
  49. return 0; }
  50.  
  51. // blah i[]
  52. fseek(in, -2, SEEK_CUR); // blah[]i
  53.  
  54. *pChar = fgetc(in); // blah[]i (gets space)
  55. fgetc(in); // blah [i] (gets i, leaves where it left off)
  56.  
  57. return 1;
  58. }
  59.  
  60.  
  61. int file_nextChar(FILE *in, int *nChar)
  62. {
  63. if(in == NULL || nChar == NULL)
  64. return -1;
  65.  
  66. *nChar = fgetc(in);
  67.  
  68. int r = fseek(in, -1, SEEK_CUR);
  69. if(r != 0)
  70. return 0;
  71. return 1;
  72. }
  73.  
  74.  
  75. int word_isLonerI(FILE *in)
  76. {
  77. if(in == NULL)
  78. return -1;
  79.  
  80. int pChar, nChar;
  81. int pC, nC;
  82.  
  83. pC = file_previousChar(in, &pChar);
  84. if(pC < 1)
  85. return 0;
  86.  
  87. nC = file_nextChar(in, &nChar);
  88. if(nC < 1)
  89. return -2;
  90.  
  91. pC = isalpha(pChar);
  92. if(pC == 0)
  93. {
  94. nC = isalpha(nChar);
  95. if(nC == 0)
  96. return 1;
  97. }
  98.  
  99. return 0;
  100. }
  101.  
  102. int main(int argc, char **argv)
  103. {
  104. FILE *in, *out;
  105. int c, r;
  106. int sBegin = 1; // beginning of sentence
  107.  
  108. if(argc < 2)
  109. return 1;
  110.  
  111. in = fopen(argv[1], "r");
  112. if(!in)
  113. return 2;
  114.  
  115. out = stdout; // there's no need to write to a file
  116. // that's what piping is for
  117. // if using *nix system file descriptor, then stdout is 1
  118.  
  119. while(1)
  120. {
  121. file_eofHandle(in);
  122.  
  123. c = fgetc(in);
  124.  
  125. if(c == '.')
  126. sBegin = 1;
  127.  
  128. if(isalpha(c) != 0) // using if((c >= 0xwhatever && c <= 0xwhatever) ||
  129. { // (blah >= lakjsdflkj...)) is harder to read
  130. // and a function should be used anyway
  131. if(sBegin != 0)
  132. {
  133. c = toupper(c);
  134. fputc(c, out);
  135. sBegin = 0;
  136. }
  137. else if(c == 'i')
  138. {
  139. r = word_isLonerI(in);
  140.  
  141. if(r < 0)
  142. {
  143. fprintf(stderr, "[-] seeking failed\n");
  144. exit(1);
  145. }
  146.  
  147. if(r == 1)
  148. fputc((int) 'I', out);
  149. else
  150. fputc((int) 'i', out);
  151. }
  152. else
  153. fputc(c, out);
  154. }
  155. else
  156. fputc(c, out);
  157. }
  158.  
  159. return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement