I used calloc to clear the string hear. is it a good idea?
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define length(x) strlen(x)
int main(int argc,char* argv[])
{
char *string;
string=(char *)malloc(20);
register char special;
register short int i=0,a=0;
register short int c;
FILE *fp;
fp=fopen(argv[1],"r");
while(1)
{
while(!feof(fp))
{
a=0;
string=(char *)calloc(20,1);//<<========<<Hear
while(!feof(fp))//
{
c=getc(fp);
special=c;
if((c>=65 && c<=122))
{
string[a]=c;
}
else if(c>=48 && c<=57)
{
string[a]=c;
}
else break;
++a;
}
i=length(string)-1;
while(i>=0)
{
putchar(string[i]);
--i;
}
if(c=='\n')break;
if((c>=0 && c<=126))
putchar(special);
}
if(c==EOF)break;
putchar('\n');
}
fclose(fp);
return 0;
}
I calloc, malloc and realloc are used for dynamic memory allocation but I do not need any dynamic memory allocation in this program. is there any other way to avoid dynamic memory allocation and hence speed up the program ?