Code For Alls..!

get the code!!

Thursday 7 September 2017

String Zig Zag Pattern

                                                                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.
Output Format:
LENGTH(S)/N + LENGTH(S)%N lines containing the desired pattern.
Boundary Conditions:
4 <= LENGTH(S) <= 500
2 <= N <= LENGTH(S)
Example Input/Output 1:
Input:
ENVIRONMENT
3
Output:
ENV
ORI
NME
*TN
Example Input/Output 2:
Input:
ENVIRONMENT
4
Output:
ENVI
MNOR
ENT*
Example Input/Output 3:
Input:
EVERYDAY
2
Output:
EV
RE
YD
YA
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--;
    }
}
}

4 comments: