How to convert 1d string to 2d array in a particular format

I want to convert a 1d character array to 2d array in a particular format given below

Input-toioynnkpheleaigshareconhtomesnlewx

I want to convert it into this format-

t o i o y

h p k n n

e l e a i

r a h s g

e c o n h

s e m o t

n l e w x

My approach is -

  cin>>n; //n is number of columns and no of rows is unknown 
  cin.ignore();
  while(n!=0)
     {
     for(i=0;i<40;i++)
        {
        for(j=0;j<n;j++)
          {
          cin.get(a[i][j]);
          if(a[i][j]=='\n')  //this condition because on entering the newline code should stop entering the string
          goto jump;
         }
        for(m=j;m>=0;m--)
         {
          i++;
          cin.get(a[i][m]);
          if(a[i][m]=='\n')
          goto jump;
        }
      }
    jump:
    for(k=0;k<i;k++)
       {
        for(l=0;l<n;l++)
          {
           cout<<a[k][l];
          }
       }

I read this problem on spoj. you can easily know the number of rows by dividing the length of the string by n which is the number of columns. follow this code and things should be clear

This code

//