1 / 2
Oct 2014

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..... smiley

#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;
}
  • created

    Oct '14
  • last reply

    Oct '14
  • 1

    reply

  • 672

    views

  • 2

    users

strlen() reads until it reaches a null terminating character. The null terminating character is stored after the string. "ABC" has a strlen of 3 but requires a length of 4 to store it.

You could avoid a lot of difficulty by processing the test cases one at a time as you input them. There is no need to take all of the input before giving output.

char s[1000005];
int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		scanf("%s", s);
		// solve the test case and print the output
	}
}

Suggested Topics

Topic Category Replies Views Activity
Off-topic 1 109 Apr 9

Want to read more? Browse other topics in Off-topic or view latest topics.