String Zig Zag Pattern
Given a string S and an integer N as the input, the program must split the string into groups whose size is N and print them as the output in separate lines in a zig zag manner. If the last group size is less than N then the remaining letters must be filled with asterisk as shown in the Example Input/Output.
Input Format:
The first line contains S.
The second line contains N.
The first line contains S.
The second line contains N.
Output Format:
LENGTH(S)/N + LENGTH(S)%N lines containing the desired pattern.
LENGTH(S)/N + LENGTH(S)%N lines containing the desired pattern.
Boundary Conditions:
4 <= LENGTH(S) <= 500
2 <= N <= LENGTH(S)
4 <= LENGTH(S) <= 500
2 <= N <= LENGTH(S)
Example Input/Output 1:
Input:
ENVIRONMENT
3
Input:
ENVIRONMENT
3
Output:
ENV
ORI
NME
*TN
ENV
ORI
NME
*TN
Example Input/Output 2:
Input:
ENVIRONMENT
4
Input:
ENVIRONMENT
4
Output:
ENVI
MNOR
ENT*
ENVI
MNOR
ENT*
Example Input/Output 3:
Input:
EVERYDAY
2
Input:
EVERYDAY
2
Output:
EV
RE
YD
YA
Example Input/Output 4:
Input:
DEOCTULWBDTMYGKWSMFNWDQIMOGINKKUHHLWTEYLCXFSMODZX
14
Output:
DEOCTULWBDTMYG
IGOMIQDWNFMSWK
NKKUHHLWTEYLCX
*******XZDOMSF
Code:
EV
RE
YD
YA
Example Input/Output 4:
Input:
DEOCTULWBDTMYGKWSMFNWDQIMOGINKKUHHLWTEYLCXFSMODZX
14
Output:
DEOCTULWBDTMYG
IGOMIQDWNFMSWK
NKKUHHLWTEYLCX
*******XZDOMSF
Code:
Java:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
String temp;
int n=sc.nextInt(),k=0,i,j,len=0;
int l=str.length();
while(l%n!=0)
{
str+='*';
l=str.length();
}
for(i=n;i<=str.length();i+=n)
{
temp=str.substring(len,i);
StringBuffer sb=new StringBuffer(temp);
if(k%2==0)
System.out.println(temp);
else
System.out.println(sb.reverse());
len+=n;
k++;
}
}
}
}
C++program:
#include <iostream>
using namespace std;
int main(int argc, char**
argv)
{
string s;
int n,c=0,l;
cin>>s;
cin>>n;
l=s.length();
while(l%n!=0)
{
s.append("*");
l=s.length();
}
int i=0,t=0;
while(i<l)
{
cout<<s[i];
c++;
if(c==n)
{
c=0;
cout<<"\n";
i=i+n;
t++;
}
else
{
if(t%2==0)
i++;
else
i--;
}
}
}
Please do comment If u have any Queries!
superb code bro
ReplyDelete
ReplyDeletea,n,l=input().strip(),int(input()),0
if(len(a)%n==0):
l=len(a)//n
else:
l=(len(a)//n)+1
a,k=a+("*"*10),0
for i in range(0,l):
if(i%2==0):
print(a[k:k+n])
k=k+n
else:
k=k+n-1
print(a[k:k-n:-1])
k=k+1
Can u pls upload this code in c language
Delete