Skip to content

Instantly share code, notes, and snippets.

@SeptiyanAndika
Last active December 25, 2017 17:29
Show Gist options
  • Select an option

  • Save SeptiyanAndika/7fd8919a9ae9f9c0246c8192c7f6c3aa to your computer and use it in GitHub Desktop.

Select an option

Save SeptiyanAndika/7fd8919a9ae9f9c0246c8192c7f6c3aa 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 = 5;
String pattern = Pad("",n,"xo");
for (int i = 0; i < n; i++) {
String print = pattern.substring(0, (2 * i) + 1);
System.out.println(Pad(print, (n-i-1)+print.length(), " "));
}
for (int i = n-1; i > 0; i--) {
String print = pattern.substring(0, (2 * i) - 1);
System.out.println(Pad(print, (n-i)+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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment