Program to draw an eqilateral triangle using input string
Logic :
D 1 -characterDee 3 - charracters
Deepa 5
Deepaks 7
1. Increament by 2
2.
StringUtils.leftPad("bat", 5," ") = " bat"
StringUtils.leftPad("bat", 4," ") = " bat"
StringUtils.leftPad("bat", 3," ") = "bat"
Program :
import org.apache.commons.lang3.StringUtils;public static void main(String [] args){
String str="Deepak";
String sTemp=null;
StringBuffer sBuff=new StringBuffer();
if(str.length/2() !=0)
str=str+"s";
for(int i=1;i<=str.length();i=i+2){
sTemp=StringUtils.mid(str, 0, i);
sTemp=StringUtils.leftPad(sTemp, str.length()," "); // StringUtils.leftPad("bat", 5) = " bat"
sBuff.append(sTemp+"\n");
}
JOptionPane.showMessageDialog(null,sBuff.toString());
}
Output :
DDee
Deepa
Deepaks
No comments:
Post a Comment