Print ZigZag String

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class ZigZazString {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ZigZazString o = new ZigZazString();
		o.doZig("NihalKurre"); // Passing string to the method
	}

	void doZig(String str) {
		for (int i = 0; i < str.length(); i++) { // Iterating through the string length
			if ((i % 4) == 0) {
				System.out.print(str.charAt(i) + "   ");

			}
		}
		System.out.println();
		for (int i = 0; i < str.length(); i++) {
			if ((i % 2) != 0) {
				System.out.print(str.charAt(i) + " ");
			}

		}
		System.out.println();
		String s = str.substring(2, str.length()); // Taking the 2 to rest of the element in given string

		for (int i = 0; i < s.length(); i++) {
			if ((i % 4) == 0) {
				System.out.print(s.charAt(i) + "   ");

			}
		}
	}
}
Output: N l r i a K r e h u

No comments:

Post a Comment