This is the code for checking if a string is a palindrome. The execution time, for sample cases, is 0.40358 seconds. What must be done to increase the performance?
int main(){
int c,flag = 0;
char *ch, *h;
ch = (char *)malloc(sizeof(char)*101);
h = ch;
while((c=getchar())!=EOF){
*ch = c;
ch++;
}
ch--;
while(ch>=h){
if(*ch-- != *h++){flag = 1; break;}
}
flag == 1?(printf("NO")):(printf("YES"));
}
This is the code with an execution time of 0.4017 seconds for same test cases:
void main()
{
char a[100],b[1000];
int n,i,j=0,f=0;
scanf("%s",a);
n=strlen(a);
for(i=n-1;i>=0;i--)
{
b[j]=a[i];
j++;
}
for(i=0;i<n-1;i++)
{
if(a[i]==b[i])
{
f++;
}
}
if(f==n-1)
{
printf("YES");
}
else
{
printf("NO");
}
}
Why is the former slower than the latter? Are the library functions faster?
main()
signatures. It is doubtful that changing the signature affects OP's goal of "What must be done to increase the performance?" \$\endgroup\$