Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. void extract_chars(char* s, char* a, char* d, char* p, char* w);
  7.  
  8. int main(int argc, char* argv[]){
  9. int count = 1;
  10. int MAX_SIZE = 100;
  11. FILE *input, *output;
  12. char *s, *letter, *digit, *punct, *white;
  13.  
  14. input = fopen(argv[1], "r");
  15. if(input == NULL){
  16. printf("Unable to read from file %s\n", argv[1]);
  17. exit(EXIT_FAILURE);
  18. }
  19.  
  20. output = fopen(argv[2], "w");
  21. if(output == NULL){
  22. printf("Unable to write to file %s\n", argv[2]);
  23. exit(EXIT_FAILURE);
  24. }
  25.  
  26. s = calloc(MAX_SIZE+1, sizeof(char));
  27.  
  28. while(fgets(s, MAX_SIZE, input) != NULL){
  29. letter = calloc(MAX_SIZE+1, sizeof(char));
  30. digit = calloc(MAX_SIZE+1, sizeof(char));
  31. punct = calloc(MAX_SIZE+1, sizeof(char));
  32. white = calloc(MAX_SIZE+1, sizeof(char));
  33.  
  34. extract_chars(s, letter, digit, punct, white);
  35.  
  36. int letterSize = strlen(letter);
  37. int digitSize = strlen(digit);
  38. int punctSize = strlen(punct);
  39. int whiteSize = strlen(white);
  40.  
  41. fprintf(output, "line %d contains:\n", count);
  42. fprintf(output, "%d ", letterSize);
  43. fprintf(output, "%s: ", letterSize > 1? "alphabetic characters" : "alphabetic character");
  44. for(int i = 0; i < letterSize; i++){
  45. fprintf(output, "%c", letter[i]);
  46. }
  47. fprintf(output, "\n%d ", digitSize);
  48. fprintf(output, "%s: ", digitSize > 1? "numeric characters" : "numeric character");
  49. for(int i = 0; i < digitSize; i++){
  50. fprintf(output, "%c", digit[i]);
  51. }
  52. fprintf(output, "\n%d ", punctSize);
  53. fprintf(output, "%s: ", punctSize > 1? "punctuation characters" : "punctuation character");
  54. for(int i = 0; i < punctSize; i++){
  55. fprintf(output, "%c", punct[i]);
  56. }
  57. fprintf(output, "\n%d ", whiteSize);
  58. fprintf(output, "%s: ", whiteSize > 1? "whitespace characters" : "whitespace character");
  59. for(int i = 0; i < whiteSize; i++){
  60. fprintf(output, "%c", white[i]);
  61. }
  62. fprintf(output, "\n");
  63.  
  64. count++;
  65. }
  66.  
  67. free(letter);
  68. free(digit);
  69. free(punct);
  70. free(white);
  71.  
  72. fclose(input);
  73. fclose(output);
  74.  
  75. output = fopen(argv[2], "r");
  76. while(fgets(s, MAX_SIZE, output) != NULL){
  77. printf("%s ", s);
  78. }
  79. fclose(output);
  80.  
  81. return EXIT_SUCCESS;
  82. }
  83.  
  84. void extract_chars(char* s, char* a, char* d, char* p, char* w){
  85. int maxSize = strlen(s);
  86. int alphaCount = 0;
  87. int digitCount = 0;
  88. int punctCount = 0;
  89. int whiteCount = 0;
  90. for(int i = 0; i < maxSize; i++){
  91. if(isalnum(*(s+i))){
  92. if(isalpha(*(s+i))){
  93. *(a+alphaCount) = *(s+i);
  94. alphaCount++;
  95. }
  96. else if(isdigit(*(s+i))){
  97. *(d+digitCount) = *(s+i);
  98. digitCount++;
  99. }
  100. }
  101. else{
  102. if(ispunct(*(s+i))){
  103. *(p+punctCount) = *(s+i);
  104. punctCount++;
  105. }
  106. else if(isspace(*(s+i))){
  107. *(w+whiteCount) = *(s+i);
  108. whiteCount++;
  109. }
  110. }
  111. }
  112. *(a+alphaCount) = '\0';
  113. *(d+digitCount) = '\0';
  114. *(p+punctCount) = '\0';
  115. *(w+whiteCount) = '\0';
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement