Write a program that reads a string and then prints a diamond shaped array based on the characters of the string.
As an example, if the string has the value "SAMPLE", then the program should print the pattern S SAS SAMAS SAMPMAS SAMPLPMAS SAMPLELPMAS SAMPLPMAS SAMPMAS SAMAS SAS S The program should work for a string up to ten characters long. If a user supplies a string that is longer than ten characters, the program should use only the first ten characters in forming the pattern.
3Answer
import java.util.Scanner;
public class StringToDiamand{
public static void main(String[] args) {
String a;
int i, j, k, l;
char ch;
Scanner in = new Scanner(System.in);
System.out.println("Enter a String:");
a = in.next();
a = a.toUpperCase();
try{
l = a.length();
if(l >10)
l = 10;
for(i = 0; i < l; i ){
for(k = 0; k < l - i - 1; k ){
System.out.print(" ");
}
for(j = 0; j <= i; j ){
ch = a.charAt(j);
System.out.print(ch);
}
for(k = i; k >= 1; k--){
ch = a.charAt(k-1);
System.out.print(ch);
}
System.out.println(" ");
}
for(i = 0; i < l - 1; i ){
for(k = 0; k <= i; k ){
System.out.print(" ");
}
for(j = 0; j < l - i -1; j ){
ch = a.charAt(j);
System.out.print(ch);
}
for(j = l - 3 - i; j >= 0; j--){
ch = a.charAt(j);
System.out.print(ch);
}
System.out.println(" ");
}
}
catch(Exception e){
System.out.println(e);
}
in.close();
}
}
- answered 4 years ago
- Milan Raj Gupta
import java.util.Scanner;
public class Sample {
public static void main(String[] args) {
String a;
int i, j, k, l;
char ch;
Scanner in = new Scanner(System.in);
a = "SAMPLE";
try{
l = a.length();
if(l >10)
l = 10;
for(i = 0; i < l; i ){
for(k = 0; k < l - i - 1; k ){
System.out.print(" ");
}
for(j = 0; j <= i; j ){
ch = a.charAt(j);
System.out.print(ch);
}
for(k = i; k >= 1; k--){
ch = a.charAt(k-1);
System.out.print(ch);
}
System.out.println(" ");
}
for(i = 0; i < l - 1; i ){
for(k = 0; k <= i; k ){
System.out.print(" ");
}
for(j = 0; j < l - i -1; j ){
ch = a.charAt(j);
System.out.print(ch);
}
for(j = l - 3 - i; j >= 0; j--){
ch = a.charAt(j);
System.out.print(ch);
}
System.out.println(" ");
}
}
catch(Exception e){
System.out.println(e);
}
in.close();
}
}
- answered 4 years ago
- Milan Raj Gupta
import java.util.Scanner;
public class StringToDiamand{
public static void main(String[] args) {
String a;
int i, j, k, l;
char ch;
Scanner in = new Scanner(System.in);
System.out.println("Enter a String:");
a = in.next();
a = a.toUpperCase();
try{
l = a.length();
if(l >10)
l = 10;
for(i = 0; i < l; i ){
for(k = 0; k < l - i - 1; k ){
System.out.print(" ");
}
for(j = 0; j <= i; j ){
ch = a.charAt(j);
System.out.print(ch);
}
for(k = i; k >= 1; k--){
ch = a.charAt(k-1);
System.out.print(ch);
}
System.out.println(" ");
}
for(i = 0; i < l - 1; i ){
for(k = 0; k <= i; k ){
System.out.print(" ");
}
for(j = 0; j < l - i -1; j ){
ch = a.charAt(j);
System.out.print(ch);
}
for(j = l - 3 - i; j >= 0; j--){
ch = a.charAt(j);
System.out.print(ch);
}
System.out.println(" ");
}
}
catch(Exception e){
System.out.println(e);
}
in.close();
}
}
- answered 4 years ago
- Community wiki
Your Answer