i m getting segmentation fault in my code.
where i m getting wrong?
MY CODE IS AS FOLLOWS:
/* program to find the next palindrome*/
/* here we have to create a next palindrome as we cant just iterate for the next palindrome
because we have constraint of digit of max number is 1000000 which mens loop must be O(n)*/
include
include
define max 10
char Input[max];
int sizeofpalin;
int CheckAll() /* if number is in format 9,99,999,9999..... then next palindrome must be start nd end with 0 nd having n(9)-1*/
{ int i;
for(i=0;i if(Input[i]!='9')
return 0;
return 1;
}
void FindPalin()
{
int i, j, mid,flag=0;
mid=sizeofpalin/2;
i=mid-1;
j=(sizeofpalin%2)?mid+1:mid;
while((i>=0)&&(Input[i]==Input[j])) /*moving away untill any number is unequal*/
{
i--;j++;
}
/*if either any digit is unequal or number entered is palindome then flag must be set*/
if(Input[i] flag=1;
/* if some digit of left side is unequal then copy dat part of number to the right side(making a next higher palindome)*/
while(i>=0)
Input[j++]=Input[i--];
/* when flag is set that mean when either entered number is palindrome of when INput[i] is less than INput[j] then following logic aply
in which we have to increas the middle digit by one and if carry generates then add that carry to left part's rightmost digit and at the end we need to copy all the left parto right side around the middle */
//puts(Input);
if(flag==1)
{
int add=1;
i=mid-1;
if(sizeofpalin%2) /*if digit is odd thn we need to increment middle one by one and take care of carry*/
{
Input[mid]=Input[mid]+1;
if(Input[mid]>='0' && Input[mid]<='9')
add=0;
else
add=1;
if(Input[mid]>='0' && Input[mid]<='9')
Input[mid]=Input[mid];
else
Input[mid]='0';
j=mid+1;
}
else
j=mid;
while(i>=0) /* adding of 1 to middle digit and pass the carry if generates and also copying the left side to the right side*/
{
Input[i]=Input[i]+add;
if(Input[i]>='0' && Input[i]<='9')
add=0;
else
add=1;
if(Input[i]>='0' && Input[i]<='9')
Input[i]=Input[i];
else
Input[i]='0';
Input[j++]=Input[i--];
}
//Input[j]='\0';
}
}
void GetNextPalin() /*finding the next palindrone and calling differnt function which are available for differnt case*/
{
int i,j;
if(CheckAll())
{
printf("1");
for(i=0;i printf("0");
printf("1");
}
else
{
FindPalin();
/* print the next highest palindrome*/
//printf("size=%d\n",sizeofpalin);
printf("%s",Input);
}
}
/* driver fumctios of other function*/
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",Input);
sizeofpalin=strlen(Input);
//printf("%d\n",sizeofpalin);
GetNextPalin();
if(T!=0)
printf("\n");
}
return 0;
}