/* Write a program to print a rectangle with a diamond shape gap exactly at the center of the
Rectangle, using an input string with odd number of characters, not exceeding 20.
For Example:-
INPUT :- HAPPY-HAPPY
OUTPUT:
Enter a word of odd number of characters
HAPPY-HAPPY
HAPPY-HAPPY
HAPPY HAPPY
HAPP APPY
HAP PPY
HA PY
H Y
HA PY
HAP PPY
HAPP APPY
HAPPY HAPPY
HAPPY-HAPPY
Answer 1.*/
import java.io.*;
class Diamond_String
{
//Data Member
public String word;
public int len;
public int spc;
//Methods
//Default Constructor
Diamond_String()
{
word="";
len=0;
spc=0;
}
//Function to Input String
public void Input()
throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Enter a word of odd number of characters");
word=br.readLine();
word=word.toUpperCase();
len=word.length();
if(len>=20)
System.out.println("Word is more than 20 characters.....Try again");
if(len%2==0)
System.out.println("Word is of even characters.....Try again");
}
while((len%2)==0);
}
//Function to Display first half of String
public void Display1()
{
int x,i,j,k;
spc=1;
x=(len/2)-1;
k=0;
System.out.println(word);
for(i=1;i<=(len/2);i++)
{
//Printing left hand side
for(j=0;j<=x;j++)
System.out.print(word.charAt(j));
x--;
//Printing spaces
for(j=1;j<=spc;j++)
System.out.print(" ");
spc+=2;
//Printing right hand side
for(j=k;j<(len/2);j++)
System.out.print(word.charAt(j));
k++;
System.out.println();
}
}
//Function to Display second half of String
public void Display2()
{
int i,j,k,x;
spc-=4;
k=(len/2)-2;
x=1;
for(i=1;i<=((len/2)-1);i++)
{
//Printing left hand side
for(j=0;j<=x;j++)
System.out.print(word.charAt(j));
x++;
//Printing spaces
for(j=spc;j>=1;j--)
System.out.print(" ");
spc-=2;
//Printing right hand side
for(j=k;j<(len/2);j++)
System.out.print(word.charAt(j));
k--;
System.out.println();
}
System.out.println(word);
}
}
class Diamond_Example
{
public void main()
throws IOException
{
Diamond_String DS=new Diamond_String();
DS.Input();
DS.Display1();
DS.Display2();
}
}
Rectangle, using an input string with odd number of characters, not exceeding 20.
For Example:-
INPUT :- HAPPY-HAPPY
OUTPUT:
Enter a word of odd number of characters
HAPPY-HAPPY
HAPPY-HAPPY
HAPPY HAPPY
HAPP APPY
HAP PPY
HA PY
H Y
HA PY
HAP PPY
HAPP APPY
HAPPY HAPPY
HAPPY-HAPPY
Answer 1.*/
import java.io.*;
class Diamond_String
{
//Data Member
public String word;
public int len;
public int spc;
//Methods
//Default Constructor
Diamond_String()
{
word="";
len=0;
spc=0;
}
//Function to Input String
public void Input()
throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Enter a word of odd number of characters");
word=br.readLine();
word=word.toUpperCase();
len=word.length();
if(len>=20)
System.out.println("Word is more than 20 characters.....Try again");
if(len%2==0)
System.out.println("Word is of even characters.....Try again");
}
while((len%2)==0);
}
//Function to Display first half of String
public void Display1()
{
int x,i,j,k;
spc=1;
x=(len/2)-1;
k=0;
System.out.println(word);
for(i=1;i<=(len/2);i++)
{
//Printing left hand side
for(j=0;j<=x;j++)
System.out.print(word.charAt(j));
x--;
//Printing spaces
for(j=1;j<=spc;j++)
System.out.print(" ");
spc+=2;
//Printing right hand side
for(j=k;j<(len/2);j++)
System.out.print(word.charAt(j));
k++;
System.out.println();
}
}
//Function to Display second half of String
public void Display2()
{
int i,j,k,x;
spc-=4;
k=(len/2)-2;
x=1;
for(i=1;i<=((len/2)-1);i++)
{
//Printing left hand side
for(j=0;j<=x;j++)
System.out.print(word.charAt(j));
x++;
//Printing spaces
for(j=spc;j>=1;j--)
System.out.print(" ");
spc-=2;
//Printing right hand side
for(j=k;j<(len/2);j++)
System.out.print(word.charAt(j));
k--;
System.out.println();
}
System.out.println(word);
}
}
class Diamond_Example
{
public void main()
throws IOException
{
Diamond_String DS=new Diamond_String();
DS.Input();
DS.Display1();
DS.Display2();
}
}
No comments:
Post a Comment