Cross String Program in Java

The Simple Cross String Program in Java

CrossString.java

package com.javatraineronline;

public class CrossString {

	public static void main(String[] args) {
		
		String data = "welcome";// Get the String to be crossed
		
		int l = data.length();// Find the length of the String

		char a[] = data.toCharArray();// Convert the String into char array
		
		// Loop through the length of the String to get crossed
		for(int i=0;i<l;i++) {
			for(int j=0;j<l;j++) {
				if(	i == j){
					System.out.print(a[i]);
				} else if(i+j == l-1 && i != j) {
					System.out.print(a[j]);
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}

Output

[su_box title=”Output for CrossString.java”]w e
e m
l o
c
l o
e m
w e[/su_box]