Program to print a string in vertical zigzag manner
Last Updated :
06 Jul, 2021
Given a string, S of size N, and a number of rows R, the task is to print the given string in a vertical zigzag fashion with respect to the given number of rows as shown in the examples.
Examples:
Input: S = “123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”, R = 9
Output:
Input: S = “AttentionReaders!Don’tStopLearning!HappyLearning!”, R = 12
Output:
Approach: In order to print the characters line by line, the idea is to find the interval between the major columns and step value for in-between columns for printing the spaces until the last character of the string is reached. Follow the steps below to solve this problem:
- Initialize a variable interval as 2*R-2 to store the gap between the major columns.
- Iterate in the range [0, R-1] using the variable i
- Initialize a variable step as interval-2*i to store step values for each row.
- Iterate in the range [i, N-1] using the variable j, incrementing j by interval in each iteration,
- Print the character, S[j].
- If the value of step lies in the range [1, interval-1] and step+j<N, then print (interval-R-i) number of spaces, then print s[j+step] and finally print (i-1) spaces.
- Else print (interval-R) number of spaces.
- Print newline after each iteration of the outer loop.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void zigzag(string s, int rows)
{
int interval = 2 * rows - 2;
for ( int i = 0; i < rows; i++) {
int step = interval - 2 * i;
for ( int j = i; j < s.length(); j = j + interval) {
cout << s[j];
if (step > 0 && step < interval
&& step + j < s.length()) {
for ( int k = 0; k < (interval - rows - i);
k++)
cout << " " ;
cout << s[j + step];
for ( int k = 0; k < i - 1; k++)
cout << " " ;
}
else {
for ( int k = 0; k < (interval - rows); k++)
cout << " " ;
}
}
cout << endl;
}
}
int main()
{
string s = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh"
"ijklmnopqrstuvwxyz" ;
int rows = 9;
zigzag(s, rows);
}
|
Java
public class GFG{
static void zigzag(String s, int rows)
{
int interval = 2 * rows - 2 ;
for ( int i = 0 ; i < rows; i++)
{
int step = interval - 2 * i;
for ( int j = i; j < s.length(); j = j + interval)
{
System.out.print(s.charAt(j));
if (step > 0 && step < interval &&
step + j < s.length())
{
for ( int k = 0 ; k < (interval - rows - i); k++)
System.out.print( " " );
System.out.print(s.charAt(j + step));
for ( int k = 0 ; k < i - 1 ; k++)
System.out.print( " " );
}
else
{
for ( int k = 0 ; k < (interval - rows); k++)
System.out.print( " " );
}
}
System.out.println();
}
}
public static void main(String args[])
{
String s = "123456789ABCDEFGHIJKLM" +
"NOPQRSTUVWXYZabcdefghi" +
"jklmnopqrstuvwxyz" ;
int rows = 9 ;
zigzag(s, rows);
}
}
|
Python3
def zigzag(s, rows):
interval = 2 * rows - 2
for i in range (rows):
step = interval - 2 * i
for j in range (i, len (s), interval):
print (s[j], end = "")
if (step > 0 and step < interval and
step + j < len (s)):
for k in range ((interval - rows - i)):
print (end = " " )
print (s[j + step], end = "")
for k in range (i - 1 ):
print (end = " " )
else :
for k in range (interval - rows):
print (end = " " )
print ()
if __name__ = = '__main__' :
s = "123456789ABCDEFGHIJKL" \
"MNOPQRSTUVWXYZabcdefghi" \
"jklmnopqrstuvwxyz"
rows = 9
zigzag(s, rows)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void zigzag( string s, int rows)
{
int interval = 2 * rows - 2;
for ( int i = 0; i < rows; i++)
{
int step = interval - 2 * i;
for ( int j = i; j < s.Length; j = j + interval)
{
Console.Write(s[j]);
if (step > 0 && step < interval &&
step + j < s.Length)
{
for ( int k = 0; k < (interval - rows - i); k++)
Console.Write( " " );
Console.Write(s[j + step]);
for ( int k = 0; k < i - 1; k++)
Console.Write( " " );
}
else
{
for ( int k = 0; k < (interval - rows); k++)
Console.Write( " " );
}
}
Console.WriteLine();
}
}
public static void Main()
{
string s = "123456789ABCDEFGHIJKLM" +
"NOPQRSTUVWXYZabcdefghi" +
"jklmnopqrstuvwxyz" ;
int rows = 9;
zigzag(s, rows);
}
}
|
Javascript
<script>
function zigzag(s,rows)
{
let interval = 2 * rows - 2;
for (let i = 0; i < rows; i++)
{
let step = interval - 2 * i;
for (let j = i; j < s.length; j = j + interval)
{
document.write(s[j]);
if (step > 0 && step < interval &&
step + j < s.length)
{
for (let k = 0; k < (interval - rows - i); k++)
document.write( "  " );
document.write(s[j + step]);
for (let k = 0; k < i - 1; k++)
document.write( "  " );
}
else
{
for (let k = 0; k < (interval - rows); k++)
document.write( "  " );
}
}
document.write( "<br>" );
}
}
let s = "123456789ABCDEFGHIJKLM" +
"NOPQRSTUVWXYZabcdefghi" +
"jklmnopqrstuvwxyz" ;
let rows = 9;
zigzag(s, rows);
</script>
|
Output
1 H X n
2 GI WY mo
3 F J V Z l p
4 E K U a k q
5 D L T b j r z
6 C M S c i s y
7 B N R d h t x
8A OQ eg uw
9 P f v
Time Complexity: O(R2*N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...