Skip to content

Instantly share code, notes, and snippets.

@SeptiyanAndika
Created December 25, 2017 17:39
Show Gist options
  • Select an option

  • Save SeptiyanAndika/4b95fd70ef2d67dff90d69c860960096 to your computer and use it in GitHub Desktop.

Select an option

Save SeptiyanAndika/4b95fd70ef2d67dff90d69c860960096 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author andika
*/
public class test {
public static void main(String[] args) {
int n = 10;
int j = n - 1;
String pattern = Pad("", n, "xo");
for (int i = 0; i < n * 2 - 1; i++) {
int maxstring = (2 * i) + 1;
int padding = n - i - 1;
if (i >= n) {
maxstring = (2 * j) - 1;
padding = n - j;
j--;
}
String print = pattern.substring(0, maxstring);
System.out.println(Pad(print, padding + print.length(), " "));
}
}
public static String Pad(String str, Integer length, String pattern) {
if (str.length() == length) {
return str;
}
return String.format("%" + (length - str.length()) + "s", "")
.replace(" ", pattern)
+ str;
}
}
@SeptiyanAndika
Copy link
Copy Markdown
Author

SeptiyanAndika commented Dec 25, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment