Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- void extract_chars(char* s, char* a, char* d, char* p, char* w);
- int main(int argc, char* argv[]){
- int MAX_SIZE = 100;
- FILE *input, *output;
- char *s, *letter, *digit, *punct, *white;
- input = fopen(argv[1], "r");
- if(input == NULL){
- printf("Unable to read from file %s\n", argv[1]);
- exit(EXIT_FAILURE);
- }
- output = fopen(argv[2], "w");
- if(output == NULL){
- printf("Unable to write to file %s\n", argv[2]);
- exit(EXIT_FAILURE);
- }
- while(fscanf(input, "%s", s) != EOF){
- fgets(s, MAX_SIZE, (FILE*)input);
- letter = calloc(MAX_SIZE+1, sizeof(char));
- digit = calloc(MAX_SIZE+1, sizeof(char));
- punct = calloc(MAX_SIZE+1, sizeof(char));
- white = calloc(MAX_SIZE+1, sizeof(char));
- extract_chars(s, letter, digit, punct, white);
- int letterSize = strlen(letter);
- int digitSize = strlen(digit);
- int punctSize = strlen(punct);
- int whiteSize = strlen(white);
- fprintf(output, "%d ", letterSize);
- fprintf(output, "%s: ", letterSize > 1? "alphabetic characters" : "alphabetic character");
- for(int i = 0; i < letterSize; i++){
- fprintf(output, "%c", letter[i]);
- }
- fprintf(output, "\n%d ", digitSize);
- fprintf(output, "%s: ", digitSize > 1? "numeric characters" : "numeric character");
- for(int i = 0; i < digitSize; i++){
- fprintf(output, "%c", digit[i]);
- }
- fprintf(output, "\n%d ", punctSize);
- fprintf(output, "%s: ", punctSize > 1? "punctuation characters" : "punctuation character");
- for(int i = 0; i < punctSize; i++){
- fprintf(output, "%c", punct[i]);
- }
- fprintf(output, "\n%d ", whiteSize);
- fprintf(output, "%s: ", whiteSize > 1? "whitespace characters" : "whitespace character");
- for(int i = 0; i < whiteSize; i++){
- fprintf(output, "%c", white[i]);
- }
- }
- free(letter);
- free(digit);
- free(punct);
- free(white);
- fclose(input);
- fclose(output);
- return EXIT_SUCCESS;
- }
- void extract_chars(char* s, char* a, char* d, char* p, char* w){
- size_t maxSize = strlen(s);
- int alphaCount = 0;
- int digitCount = 0;
- int punctCount = 0;
- int whiteCount = 0;
- for(int i = 0; i < maxSize; i++){
- if(isalnum(s[i])){
- if(isalpha(s[i])){
- *(a+alphaCount) = s[i];
- alphaCount++;
- }
- else if(isdigit(s[i])){
- *(d+digitCount) = s[i];
- digitCount++;
- }
- }
- else{
- if(ispunct(s[i])){
- *(p+punctCount) = s[i];
- punctCount++;
- }
- else if(isspace(s[i])){
- *(w+whiteCount) = s[i];
- whiteCount++;
- }
- }
- }
- *(a+alphaCount) = '\0';
- *(d+digitCount) = '\0';
- *(p+punctCount) = '\0';
- *(w+whiteCount) = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement