the following code (my solution for to and fro problem) is working correct on my compiler as well as on ideone it's giving correct output ..but on submitting here on SPOJ ,it shows wrong output..
Can anyone help me with this code to detect what's the problem?
here's the problem link:
http://www.spoj.com/problems/TOANDFRO/
// CODE for TO AND FRO PROBLEM
include "stdio.h"
include "string.h"
void reverse(char*arr,char*f,char*e)
{
char temp;
while(f<e)
{
temp=*f;
*f=*e;
*e=temp;
f++;
e--;
}
}
int main()
{
int r, n,i,j,c;
while(1)
{
char str[202],p,q;
char ans[15][15]={'\0'};
//fflush(stdin);
//gets(str);
scanf("%d",&n);
if(n==0)
break;
scanf("%s",str);
p=str;
q=str;
c=1;
i=0;j=0;
while(*p!='\0')
{
if(c==n+1)
{
c=1;
i++;
j=0;
if(i%2!=0)
{
reverse(str,p,p+n-1);
}
}
ans[i][j++]=*p;
c++;
p++;
}
int k;
r=strlen(str)/n;
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
*(q+k++)=ans[j][i];
}
*(q+k)='\0';
printf("%s\n",q);
}
}