#include <stdio.h>
#include<string.h>
#include<stdlib.h>
char* encode(char* buf,int line)
{
int len=strlen(buf);
int nlen=len;
if(len%line)
nlen=len+(line-len%line);
char* tmp=(char*)malloc(nlen+1);
strcpy(tmp,buf);
for(int i=strlen(tmp);i<nlen;i++)
{
tmp[i]='*';
}
tmp[nlen]='\0';
char* de=(char*)malloc(nlen+1);
char* se=de;
char(*rp)[line]=tmp;
for(int i=0;i<line;i++)
{
for(int j=0;j<(nlen/line);j++)
{
*se++=rp[j][i];
}
}
return de;
}
char* decode(char* buf,int line)
{
int len=strlen(buf);
char* tmp=(char*)malloc(len+1);
char* str=tmp;
int row=len/line;
char(*rp)[line]=buf;
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
{
*str++=rp[j][i];
}
}
while((*--str)=='*');
*(++str)='\0';
return tmp;
}
int main(int argc,char* argv[])
{
char* str="china is great";
char* s=encode(str,3);
printf("%s\n",s);
char* ss=decode(s,strlen(s)/3);
printf("%s\n",ss);
return 0;
}