概要
123 456 789
上記の値を持つ string を
147 258 369
このように変換する関数
private static string TransposeString( string input ) { var lines = input.Split( '\n' ); var rowCount = lines.Length; var columnCount = lines[ 0 ].Length; var matrix = new char[ rowCount, columnCount ]; // 文字列を2次元の行列に変換 for ( var i = 0; i < rowCount; i++ ) { for ( var j = 0; j < columnCount; j++ ) { matrix[ i, j ] = lines[ i ][ j ]; } } // 行列を転置 var transposedMatrix = new char[ columnCount, rowCount ]; for ( var i = 0; i < columnCount; i++ ) { for ( var j = 0; j < rowCount; j++ ) { transposedMatrix[ i, j ] = matrix[ j, i ]; } } // 転置された行列を文字列に変換 var output = new StringBuilder(); for ( var i = 0; i < columnCount; i++ ) { for ( var j = 0; j < rowCount; j++ ) { output.Append( transposedMatrix[ i, j ] ); } output.AppendLine(); } return output.ToString().TrimEnd(); }