All pastes #608116 Raw Edit

Random app

public text v1 · immutable
·published 2007-07-08 02:01 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define      MAX_WORD_LEN   50
#define      MAX_NAME_LEN   80
#define      MAX_LINE_LEN   1000
#define      BUFFER_SIZE      MAX_LINE_LEN + 1


void revprint(const char *str, FILE *fd)
{
   char s[MAX_WORD_LEN + 1] = "";
   int index = 0;
   static const char seps[] = " \n\t,.:;!?";

   while (*str && strchr(seps, *str))
      str++;
   
   if (!*str) {
      return;
   }

   while (!strchr(seps, *str))
      s[index++] = *str++;
   
   s[index] = '\0';

   if (*str == '\0') {
      fprintf(fd, "%s ", s);
      return;
   }

   revprint(str, fd);
   fprintf(fd, "%s ", s);
}

int main(int argc, char **argv)
{
   FILE *fs, *fd;
   char sfname[MAX_NAME_LEN + 1];
   char dfname[MAX_NAME_LEN + 1];
   char buffer[BUFFER_SIZE];

   if (argc != 3) {
      printf("enter file name to convert : ");
      gets(sfname);
      printf("enter new file name        : ");
      gets(dfname);
   }
   else {
      strcpy(sfname, argv[1]);
      strcpy(dfname, argv[2]);
   }

   fs = fopen(sfname, "r");
   
   if (fs == NULL) {
      fprintf(stderr, "cannot open %s!\n", sfname);
      return 1;
   }

   fd = fopen(dfname, "w");
   
   if (fd == NULL) {
      fprintf(stderr, "cannot create %s!\n", dfname);
      fclose(fs);
      return 2;
   }

   while (fgets(buffer, BUFFER_SIZE, fs)) {
      revprint(buffer, fd);
      fprintf(fd, "\n");
   }

   fclose(fs);
   fclose(fd);

   return 0;
}