I have impleted the "The Next Palindrome" code using pointers.
The Code works very well in my computer.
For any size input , the execution time(i am getting) is just below 5sec.
But the thing is that i am getting a segmentation fault when submitted.
So my doubt is ....
Whether we are allowed to implement the code using pointers??
and
Let me know ,If any one find any other issue in this program???
I have mentioned my code below.
Thanks in Advance.....
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char **cpy;
void StrInc(int i,int j)
{
char *result = malloc(10);
char *padding = "1";
if(cpy[i][j]!='9')
{
cpy[i][j]++;
return;
}
else if((cpy[i][j]=='9'))
{
while(j)
{
cpy[i][j]='0';
if(cpy[i][j-1]!='9')
{
cpy[i][j-1]++;
return;
}
else
{
j--;
if(j==0)
{
if(cpy[i][j]!='9')
{
cpy[i][j]++;
}
else
{
//printf("now in the strcpy block");
strcpy(result,padding);
cpy[i][j]='0';
strcat(result,cpy[i]);
cpy[i] = result;
//printf("%s",cpy);
return;
}
}
}
}
}
//StrInc(str,j+1);
}
int main()
{
int i,t,j;
scanf("%d",&t);
cpy = malloc(10);
for(i=0;i<t;i++)
{
cpy[i] = malloc(1000000);
// printf("enter whatever thing u want\n");
scanf("%s",cpy[i]);
// cpy = str;
}
for(i=0;i<t;i++)
{
while(1)
{
j=strlen(cpy[i])-1;
StrInc(i,j);
// printf("the cpy value is %s \n",cpy);
j=0;
while(j<=(strlen(cpy[i]))/2)
{
// printf("the value of j is %d\n",j);
// printf("the value of strlen(cpy)/2 is %d\n",((strlen(cpy))/2));
//printf("the cpy[j] is %c \n",cpy[j]);
//printf("the cpy[strlen(cpy)-1-j] is %c \n",cpy[strlen(cpy)-1-j]);
if(cpy[i][j]==cpy[i][strlen(cpy[i])-1-j])
{
// printf("the value of j is %d\n",j);
if(j==((strlen(cpy[i])/2)-1))
{
printf("%s\n",cpy[i]);
goto xxy;
}
}
else
{
goto xxx;
}
j++;
}
xxx: ;
}
xxy: ;
}
return 0;
}