Skip to main content
deleted 26 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

Java 1011, 189 159159 136 bytes

s->c->{var r="";intint p=0,i;forp=0;for(var a:s.split("\\|")){forSystem.out.println(i=p;i-->0;r+="" ");r+=a;for(p+=a.lengthrepeat(p);i++<s+a+" ".repeat(s.replace("|","").length()-p;r+=" "(p+=a.length();r+=c+"\n";}return r;)+1)+c);}

-30 bytes converting Java 7 to Java 10 and optimizing the loops.
-23 bytes converting Java 10 to Java 11, so String#repeat builtin can be used.

Try it online.Try it online.

Explanation:

s->c->{                     // Method with String & char parameters and Stringno return-type
  var r="";                 //  Result-String, starting empty
  int p=0,p=0;                  //  Position-integer, starting at 0
      i; for(var a:s.split("\\|")) //  Loop over the parts split by "|":
    System.out.println(     //  Index integer
Print with for(vartrailing anewline:s.split("\\|")){// 
  Loop over the parts split" by".repeat(p) "|"
    for(i=p;i-->0;r+=" ");   //   Add `p` amount of spaces 
 to the result-String
   +a r+=a;                   //   Add plus the current part 
 to the result-String
   " for".repeat(s.replace("|","").length()-(p+=a.length();)+1)
      //   Add the length of the current part to the position-integer
        i++<s.replace("|","").length()-p;r+=" "); //    plus the correct trailing amount of spaces
                            //   Add (and add the row'slength trailingof spacesthe current part to the`p` result-Stringat the
    r+=c+"\n";}             //   Add the character and a new-line to the result-String//    same time)
  return r;   +c);}                 //  Return  plus the result-Stringcurrent comment character

Java 10, 189 159 bytes

s->c->{var r="";int p=0,i;for(var a:s.split("\\|")){for(i=p;i-->0;r+=" ");r+=a;for(p+=a.length();i++<s.replace("|","").length()-p;r+=" ");r+=c+"\n";}return r;}

-30 bytes converting Java 7 to Java 10 and optimizing the loops.

Try it online.

Explanation:

s->c->{                     // Method with String & char parameters and String return-type
  var r="";                 //  Result-String, starting empty
  int p=0,                  //  Position-integer, starting at 0
      i;                    //  Index integer
  for(var a:s.split("\\|")){//  Loop over the parts split by "|"
    for(i=p;i-->0;r+=" ");  //   Add `p` amount of spaces to the result-String
    r+=a;                   //   Add the current part to the result-String
    for(p+=a.length();      //   Add the length of the current part to the position-integer
        i++<s.replace("|","").length()-p;r+=" ");
                            //   Add the row's trailing spaces to the result-String
    r+=c+"\n";}             //   Add the character and a new-line to the result-String
  return r;}                //  Return the result-String

Java 11, 189 159 136 bytes

s->c->{int p=0;for(var a:s.split("\\|"))System.out.println(" ".repeat(p)+a+" ".repeat(s.replace("|","").length()-(p+=a.length())+1)+c);}

-30 bytes converting Java 7 to Java 10 and optimizing the loops.
-23 bytes converting Java 10 to Java 11, so String#repeat builtin can be used.

Try it online.

Explanation:

s->c->{                     // Method with String & char parameters and no return-type
  int p=0;                  //  Position-integer, starting at 0
  for(var a:s.split("\\|")) //  Loop over the parts split by "|":
    System.out.println(     //   Print with trailing newline: 
      " ".repeat(p)         //    `p` amount of spaces 
      +a                    //    plus the current part 
      " ".repeat(s.replace("|","").length()-(p+=a.length())+1)
                            //    plus the correct trailing amount of spaces
                            //    (and add the length of the current part to `p` at the
                            //    same time)
      +c);}                 //    plus the current comment character
Commonmark migration
Source Link

#Java 10, 189 159 bytes

Java 10, 189 159 bytes

s->c->{var r="";int p=0,i;for(var a:s.split("\\|")){for(i=p;i-->0;r+=" ");r+=a;for(p+=a.length();i++<s.replace("|","").length()-p;r+=" ");r+=c+"\n";}return r;}

-30 bytes converting Java 7 to Java 10 and optimizing the loops.

Try it online.

Explanation:

s->c->{                     // Method with String & char parameters and String return-type
  var r="";                 //  Result-String, starting empty
  int p=0,                  //  Position-integer, starting at 0
      i;                    //  Index integer
  for(var a:s.split("\\|")){//  Loop over the parts split by "|"
    for(i=p;i-->0;r+=" ");  //   Add `p` amount of spaces to the result-String
    r+=a;                   //   Add the current part to the result-String
    for(p+=a.length();      //   Add the length of the current part to the position-integer
        i++<s.replace("|","").length()-p;r+=" ");
                            //   Add the row's trailing spaces to the result-String
    r+=c+"\n";}             //   Add the character and a new-line to the result-String
  return r;}                //  Return the result-String

#Java 10, 189 159 bytes

s->c->{var r="";int p=0,i;for(var a:s.split("\\|")){for(i=p;i-->0;r+=" ");r+=a;for(p+=a.length();i++<s.replace("|","").length()-p;r+=" ");r+=c+"\n";}return r;}

-30 bytes converting Java 7 to Java 10 and optimizing the loops.

Try it online.

Explanation:

s->c->{                     // Method with String & char parameters and String return-type
  var r="";                 //  Result-String, starting empty
  int p=0,                  //  Position-integer, starting at 0
      i;                    //  Index integer
  for(var a:s.split("\\|")){//  Loop over the parts split by "|"
    for(i=p;i-->0;r+=" ");  //   Add `p` amount of spaces to the result-String
    r+=a;                   //   Add the current part to the result-String
    for(p+=a.length();      //   Add the length of the current part to the position-integer
        i++<s.replace("|","").length()-p;r+=" ");
                            //   Add the row's trailing spaces to the result-String
    r+=c+"\n";}             //   Add the character and a new-line to the result-String
  return r;}                //  Return the result-String

Java 10, 189 159 bytes

s->c->{var r="";int p=0,i;for(var a:s.split("\\|")){for(i=p;i-->0;r+=" ");r+=a;for(p+=a.length();i++<s.replace("|","").length()-p;r+=" ");r+=c+"\n";}return r;}

-30 bytes converting Java 7 to Java 10 and optimizing the loops.

Try it online.

Explanation:

s->c->{                     // Method with String & char parameters and String return-type
  var r="";                 //  Result-String, starting empty
  int p=0,                  //  Position-integer, starting at 0
      i;                    //  Index integer
  for(var a:s.split("\\|")){//  Loop over the parts split by "|"
    for(i=p;i-->0;r+=" ");  //   Add `p` amount of spaces to the result-String
    r+=a;                   //   Add the current part to the result-String
    for(p+=a.length();      //   Add the length of the current part to the position-integer
        i++<s.replace("|","").length()-p;r+=" ");
                            //   Add the row's trailing spaces to the result-String
    r+=c+"\n";}             //   Add the character and a new-line to the result-String
  return r;}                //  Return the result-String
deleted 748 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 710, 189189 159 bytes

String c(String s,char c){String r="";int x=0,i;for(String a:s.split("\\|")){for(i=0;i++<x;r+=" ");r+=a;x+=a.length();for(i=-1;i++<s.replace("|","").length()-x;r+=" ");r+=c+"\n";}return r;}
s->c->{var r="";int p=0,i;for(var a:s.split("\\|")){for(i=p;i-->0;r+=" ");r+=a;for(p+=a.length();i++<s.replace("|","").length()-p;r+=" ");r+=c+"\n";}return r;}

Ungolfed & test cases:-30 bytes converting Java 7 to Java 10 and optimizing the loops.

Try it here.Try it online.

class M{
  static String c(String s, char c){
    String r = "";
    int x = 0,
        i;
    for(String a : s.split("\\|")){
      for(i = 0; i++ < x; r += " ");
      r += a;
      x += a.length();
      for(i = -1; i++ < s.replace("|", "").length() - x; r += " ");
      r += c + "\n";
    }
    return r;
  }

  public static void main(String[] a){
    System.out.println(c("a|bc|d|e|fgh|ij|k|l|mn|op", '#'));
    System.out.println(c("Hello|World", '/'));
    System.out.println(c("a|b|c|d|e|f|g", ','));
    System.out.println(c("abcdefg", ':'));
    System.out.println(c("4|8|15|16|23|42", '%'));
    System.out.println(c("E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last!", '!'));
    System.out.println(c("This|Code|has||empty||sections", '@'));
  }
}

OutputExplanation:

a                #
 bc              #
   d             #
    e            #
     fgh         #
        ij       #
          k      #
           l     #
            mn   #
              op #

Hello      /
     World /

a       ,
 b      ,
  c     ,
   d    ,
    e   ,
     f  ,
      g ,

abcdefg :

4          %
 8         %
  15       %
    16     %
      23   %
        42 %

E                                                   !
 ac                                                 !
   h s                                              !
      ecti                                          !
          on is                                     !
                one c                               !
                     haracte                        !
                            r longer                !
                                     than the       !
                                              last! !

This                     @
    Code                 @
        has              @
                         @
           empty         @
                         @
                sections @
s->c->{                     // Method with String & char parameters and String return-type
  var r="";                 //  Result-String, starting empty
  int p=0,                  //  Position-integer, starting at 0
      i;                    //  Index integer
  for(var a:s.split("\\|")){//  Loop over the parts split by "|"
    for(i=p;i-->0;r+=" ");  //   Add `p` amount of spaces to the result-String
    r+=a;                   //   Add the current part to the result-String
    for(p+=a.length();      //   Add the length of the current part to the position-integer
        i++<s.replace("|","").length()-p;r+=" ");
                            //   Add the row's trailing spaces to the result-String
    r+=c+"\n";}             //   Add the character and a new-line to the result-String
  return r;}                //  Return the result-String

#Java 7, 189 bytes

String c(String s,char c){String r="";int x=0,i;for(String a:s.split("\\|")){for(i=0;i++<x;r+=" ");r+=a;x+=a.length();for(i=-1;i++<s.replace("|","").length()-x;r+=" ");r+=c+"\n";}return r;}

Ungolfed & test cases:

Try it here.

class M{
  static String c(String s, char c){
    String r = "";
    int x = 0,
        i;
    for(String a : s.split("\\|")){
      for(i = 0; i++ < x; r += " ");
      r += a;
      x += a.length();
      for(i = -1; i++ < s.replace("|", "").length() - x; r += " ");
      r += c + "\n";
    }
    return r;
  }

  public static void main(String[] a){
    System.out.println(c("a|bc|d|e|fgh|ij|k|l|mn|op", '#'));
    System.out.println(c("Hello|World", '/'));
    System.out.println(c("a|b|c|d|e|f|g", ','));
    System.out.println(c("abcdefg", ':'));
    System.out.println(c("4|8|15|16|23|42", '%'));
    System.out.println(c("E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last!", '!'));
    System.out.println(c("This|Code|has||empty||sections", '@'));
  }
}

Output:

a                #
 bc              #
   d             #
    e            #
     fgh         #
        ij       #
          k      #
           l     #
            mn   #
              op #

Hello      /
     World /

a       ,
 b      ,
  c     ,
   d    ,
    e   ,
     f  ,
      g ,

abcdefg :

4          %
 8         %
  15       %
    16     %
      23   %
        42 %

E                                                   !
 ac                                                 !
   h s                                              !
      ecti                                          !
          on is                                     !
                one c                               !
                     haracte                        !
                            r longer                !
                                     than the       !
                                              last! !

This                     @
    Code                 @
        has              @
                         @
           empty         @
                         @
                sections @

#Java 10, 189 159 bytes

s->c->{var r="";int p=0,i;for(var a:s.split("\\|")){for(i=p;i-->0;r+=" ");r+=a;for(p+=a.length();i++<s.replace("|","").length()-p;r+=" ");r+=c+"\n";}return r;}

-30 bytes converting Java 7 to Java 10 and optimizing the loops.

Try it online.

Explanation:

s->c->{                     // Method with String & char parameters and String return-type
  var r="";                 //  Result-String, starting empty
  int p=0,                  //  Position-integer, starting at 0
      i;                    //  Index integer
  for(var a:s.split("\\|")){//  Loop over the parts split by "|"
    for(i=p;i-->0;r+=" ");  //   Add `p` amount of spaces to the result-String
    r+=a;                   //   Add the current part to the result-String
    for(p+=a.length();      //   Add the length of the current part to the position-integer
        i++<s.replace("|","").length()-p;r+=" ");
                            //   Add the row's trailing spaces to the result-String
    r+=c+"\n";}             //   Add the character and a new-line to the result-String
  return r;}                //  Return the result-String
Forgot two test cases
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading