# Character Cross

I was expecting to post something more complex as my first puzzle in PCG, but a particular, uh... homework question on Stack Overflow inspired me to post this. They want to:

print the following pattern for any given word that contains odd number of letters:

P           M
R       A
O   R
G
O   R
R       A
P           M


Notice that letters are a knight's move apart in the pattern you need to print. So, every other column is empty. -- (Thanks xnor for pointing this out.)

## Rules

1. Using C++ is prohibited. As I may link this question there.
2. You may use stdout, or any means of quickly outputting a string (e.g. alert() in JavaScript).
3. As always, shortest code wins.
• This is in the related posts on stack overflow: stackoverflow.com/q/5508110 Oct 29, 2014 at 11:26
• @flawr I think it means odd, as in not an even number. Oct 29, 2014 at 14:28
• God I am stupid, I thought it was kind of an obscure computer scientist abbreviation=) Oct 29, 2014 at 14:34
• @jpjacobs: Bytes, unless the question explicitly says otherwise. Oct 29, 2014 at 17:20
• Whole program or just a function? (real smart of me to answer first and make this question later...) Oct 29, 2014 at 20:23

## C# 208

static void Main()
{
int n=s.Length;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)t+=s[i];
else if(i==n-j-1)t+=s[n-i-1];
t+=" ";
}
t+="\n";
}
Console.WriteLine(t);
}

• Save 11 precious characters with t+=i==j?s[i]:i==n-j-1?s[n-i-1]:"";. Oct 29, 2014 at 11:22
• Can you remove those newlines? Jun 23, 2017 at 12:53

:w,:l,{l,{.[.l(\-]2$?)!w@[=32]=}%\;''+' '*}%n*  No matter how long, there's always gotta be an answer in... # Java - 289 234 bytes public static void main(String[]a){int l=a.length-1,i=0,j;for(;i<=l;i++){for(j=0;j<=l;j++){if(j==i)System.out.print(a[i]);else if(i==l-j)System.out.print(a[j]);else System.out.print(" ");System.out.print(" ");}System.out.println();}}}  Ungolfed:  class A { public static void main(String[] a) { int l = a.length - 1, i = 0, j; for (; i <= l; i++) { for (j=0; j <= l;j++) { if (j == i) System.out.print(a[i]); else if (i == l-j) System.out.print(a[j]); else System.out.print(" "); System.out.print(" "); } System.out.println(); } } }  Output, lousily done, is: P M R A O R G O R R A P M  Added the import java.util.Scanner inside the code because I never remember if the imports count towards the byte count... Damn, I really suck at this. • Imports do count. This is partly because of Python's import-and-alias syntax: from math import floor as f which is a bit cheaty – user16402 Oct 29, 2014 at 15:54 • You should be able to save a bunch of characters by combining all the System.out.print calls into one, using a couple of ternary operators. Oct 30, 2014 at 16:29 • @DLosc Could you give me a few examples? Oct 30, 2014 at 18:28 • Yes, actually Geobits' answer is a perfect example--see the contents of the System.out.print call at the end. Oct 31, 2014 at 0:13 • @RodolfoDias Don't feel that way. My first few golfs in Java were terrible, and I can still normally shave a decent chunk off my "first revisions" if I look hard enough ;) Oct 31, 2014 at 13:28 # C# (192 / 170) using System;class P{static void Main(){var s=Console.ReadLine();int x,y,l=s.Length;for(x=0;x<l;x++){for(y=0;y<l;y++)Console.Write(x==y||l-x==y+1?s.Substring(x,1):" ");Console.Write("\n");};}}  Or, as "Main() only": static void Main(){var s=Console.ReadLine();int x,y,l=s.Length;for(x=0;x<l;x++){for(y=0;y<l;y++)Console.Write(x==y||l-x==y+1?s.Substring(x,1):" ");Console.Write("\n");};}  # QBasic, 91 bytes INPUT w$
l=LEN(w$) FOR i=1TO l c$=MID$(w$,i,1)
LOCATE i+1,i*2
?c$LOCATE l-i+2,i*2 ?c$
NEXT


Loops through the input word and uses the LOCATE statement to place each letter (twice) on the screen.

# K (oK), 31 bytes

Solution:

c$a||a:(2*#x)$(-1-2*!#x)$,:'x:  Try it online! Explanation: Generate the diagonal, and or with the reverse of the diagonal c$a||a:(2*#x)$(-1-2*!#x)$,:'x: / the solution
x: / save input as x
,:'   / enlist (,:) each-both (')
$/ pad (negative is right-pad) ( ) / do this together #x / count (#) length of x ! / range (!) 0..this count 2* / double it -1- / subtract from -1$                 / pad
(    )                  / do this together
#x                   / count (#) length of x
2*                     / multiply by 2
a:                        / save as a
|                          / reverse (|) it
a|                           / or (|) with a
c$/ cast to characters  # K4, 26 bytes Solution: a||a:(2*#x)$(-1-2*!#x)$$x:  Explanation: Similar to my oK answer but a little shorter: a||a:(2*#x)(-1-2*!#x)$$x: / the solution
x: / save input as x
$/ string ($) breaks into char lists
$/ cast ($)
(        )     / do this together
#x      / count (#) length of x
!        / range (!) 0..this count
2*         / double it
-1-           / subtract from -1
")$t=" ";  Run as pipe with -nR or try it online. # Python 3, 176 bytes w=input();l=len(w);d=l//2;n=' ';x=range(d) for i in x:print(n*i*2+w[i],n*(l+2-i*4),w[-1*i-1]) print(n*(l-1)+w[d]) for i in x:print(n*(l-i*2-3)+w[-1*(d+i+2)],n*(i*4+1),w[d+i+1])  Try it online! This is super stupid, any improvements that you notice are welcome # TI-Basic, 75 bytes Input Str1 length(Str1→L For(I,1,L " For(J,1,L If max(J={I,1-I+L:Then Ans+sub(Str1,J,1)+" Else Ans+" End End Disp Ans End  There is 1 space at the end of lines 4 and 7, and there are 2 spaces at the end of line 9. Outputs a leading space at the beginning of each line. Alternatively, a larger but faster method (100 bytes): Input Str1 length(Str1→L " For(I,1,L Ans+" →Str2 End For(I,1,L min(I,1-I+L→K 2(max(L,2)-Ans Disp sub(Str2+sub(Str1,K,1)+Str2,Ans+4,Ans)+sub(Str1,L-K+1,1 End  There is 1 space at the end of line 3. Outputs two leading spaces if the input has a length of 1. # Jelly, 10 bytes ṪṭaɗƤ⁶»Ṛ$G


Try it online!

Erik's existing answer ties with minor modifications to also use G: LḶ⁶ẋ;"µ»ṚG.

    Ƥ         For each prefix of the input,
Ṫ             remove the last element
ṭ            and append it back onto
aɗ ⁶        the remaining elements replaced with spaces.
»       Take the maximum of each element of that and
Ṛ\$     its reverse.
G    Grid format: join each row on spaces then join the rows on newlines.


# C++:

#include <iostream>
#include <string>

int main()
{
char a[] = {'P','R','O','G','R','A','M'};
int length = sizeof(a)/sizeof(a[0]);
int l = length - 1, i, j;
for (i=0; i <= l; i++) {
for (j=0; j <= l;j++) {
if (j == i)
std::cout<<a[i];
else if (i == l-j)
std::cout<<a[j];
else
std::cout<<"s";
}
std::cout<<"\n";
}
}
`
• Welcome to PPCG! This is code-golf which means that the goal is to solve the problem in as few bytes as code as possible. As per our help centre every answer should make a reasonable stab at the given winning criterion (within the limits of their language), which would mean at least removing unnecessary whitespace and using single-character variable names. You can always include a readable/"ungolfed" version in addition to the competitive one. Once you've done this, please also include the byte count of your code in the header. Apr 7, 2016 at 9:04
• And anyways, per the rules, using C++ is prohibited. Jun 23, 2017 at 12:58