Advertisement
Guest User

Untitled

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