I wrote a program which finds the next palindrome. First, it takes an input specifying the number of test cases. The code runs fine in Visual C++, but am not able to run it in GCC.

I have never used GCC before so unaware with its functioning. But am expecting a standard code should run in all the standard compilers. I'm submitting the solution on a website. What option should I select, C++(gcc-4.0.0-8) or C++(gcc-4.3.2)?

#include<iostream>
using namespace std;
int palnext(char *);
int main()
{
	 char input[100000];
	 int n;
	 cin>>n;
	 while(n!=0)
	 {
		 cin>>input;
		 palnext(input);
		 n--;
		 cout<<input<<"\n";
	 }
	 cin.get();
	 cin.get();
	 return 0;
}
int palnext(char* a)
{
	int half,temp,j=0,i=0;
	while(a[i]!='\0')
		i++;
	half=i/2;
	int flag=1,flag2=1;
	temp=i-1;
	while(j<half)
	{
		flag=1;
		while(a[j]>a[temp])
		{	
			flag2=0;
			a[temp]++;
		}
		while(a[j]<a[temp])
		{
			if(flag)
			{
				a[temp-1]++;
				flag=0;
			}
			flag2=0;
			a[temp]--;
		}
		j++;
		temp--;
	}
	if(flag2)
	{
		if(i%2==0)
		{
			a[half]++;
			a[half+1]++;
		}
		else
			a[half]++;
	}
	return 0;
}

Is it C99strict? Also, any suggestions regarding code optimization is also welcome.

Please describe 'not able to run it'.
I compiled your code and ran it with the following input

1
2345431

and got in return

2345432

As I would expect.

Thanks L7Sqr, its working fine using GCC too.

But as I mentioned, when I'm trying to submit it on a site, its giving a runtime error.
The solution is checked automatically based on the language selected, I tried C++(gcc-4.0.0-8), C++(gcc-4.3.2) and even C99strict.

It's hard to determine what 'runtime error' means with respect to an online judging application. Perhaps there are some rules you are not following? Is there a time constraint you have not satisfied?
In general, these competitions tend to be strict to the letter of the description. I'd suggest you run with some sample input (if there is any) to see what might be the problem.

Someone please give a look to this problem.. http://www.codechef.com/problems/PALIN/

I tried submitting the above solution but its giving a runtime error.

104 posts and that's the information you give to a problem -- "gives a run-time error"? :icon_rolleyes:

My car makes a squirsh noise. Please tell me how to fix it...

Hehe WaltP, you are right that doesn't makes sense.

But this is the only information I'm getting while trying to submit the solution, not any more.

And the solution I posted above is working on my end. Please refer to the first post for more details.

Edited 5 Years Ago by vidit_X: n/a

Hmm I have a version of the palindrome here, I think it would be interesting to see the performance difference between this version and yours (this one uses the STL).

template<class Int_Type>
Int_Type NextPalindromeNumber(Int_Type number)
{
	std::stringstream ss;
	std::string numberString;
	do
	{
		number++;
		ss.clear();
		ss.str("");
		ss << number;
		ss >> numberString;
	}while(!std::equal(numberString.begin(), numberString.begin()+numberString.size()/2, numberString.rbegin()));
	return number;
}

Thanks psudeorandom21 for your solution. But can you please answer some of my questions in first post.

Tried G++, its working fine at my end. But when trying to submit the above solution to the website, its giving a runtime error with no other information.

Limit on the amount of memory you can use perhaps? Hmm... Maybe the system has a tiny amount of stack space available? Where you submit this code, does it run your program and give it input? Maybe it feeds it erroneous input, and if so you will need to handle those errors. A post on that website says something about the possibility of the website using excessive line breaks.

Edited 5 Years Ago by pseudorandom21: n/a

This article has been dead for over six months. Start a new discussion instead.