# Encryption HackerRank Solution in C, C++, Java, Python

An English text needs to be encrypted using the following encryption scheme.

First, the spaces are removed from the text. Let  be the length of this text.

Then, characters are written into a grid, whose rows and columns have the following constraints:

Example

After removing spaces, the string is  characters long.  is between  and , so it is written in the form of a grid with 7 rows and 8 columns.

```ifmanwas

meanttos

tayonthe

groundgo

dwouldha

vegivenu

sroots```
• Ensure that
• If multiple grids satisfy the above conditions, choose the one with the minimum area, i.e. .

The encoded message is obtained by displaying the characters of each column, with a space between column texts. The encoded message for the grid above is:

imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau

Create a function to encode a message.

Function Description

Complete the encryption function in the editor below.

encryption has the following parameter(s):

• string s: a string to encrypt

Returns

• string: the encrypted string

Input Format

One line of text, the string

Constraints

contains characters in the range ascii[a-z] and space, ascii(32).

Sample Input

`haveaniceday`

Sample Output 0

`hae and via ecy`

## Encryption HackerRank Solution in C

```#include<stdio.h>
#include<math.h>
int main()
{
char s[82];
scanf("%s",s);
int i,c,r,n,j,k,sq;
for(i=0;s[i];++i);
sq=sqrt(i);
if(sq*sq==i)
{
c=r=sq;
}
else
{
r=sq;c=sq+1;
if(r*c<i){r=sq+1;c=sq+1;}
}

for(j=0;j<c;++j)
{
for(k=0;k<r;++k)if(j+k*c<i)printf("%c",s[j+k*c]);
printf(" ");
}
return 0;
}```

## Encryption HackerRank Solution in C++

```#include <cstring>
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

#define MAXN 1000
char str[MAXN];
char res[MAXN][MAXN];
int gr,gc;
int r,c;

int main(){
scanf("%s",str);
int len = strlen(str); int DIFF = 1<<20; int AREA = 1<<20;
for(int i=1; i<=100; ++i) {
if(i*i>=len) {
r = i; c = i; break;
}
if(i*(i+1)>=len){
r = i; c = i+1; break;
}
}
int id = 0;
for(int i=0; i<MAXN; ++i) for(int j=0; j<MAXN; ++j) res[i][j] = '\0';
for(int i=0; i<r; ++i) for(int j=0; j<c; ++j) res[i][j] = str[id++];
for(int i=0; i<c; ++i) {
for(int j=0; res[j][i]!='\0'; ++j) printf("%c",res[j][i]);
printf(" ");
}

return 0;
}```

## Encryption HackerRank Solution in Java

```import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
String s= scan.next();
int wid,len;
int l=s.length();
double f=Math.sqrt(l);
int test=(int)f;
if(test*test==l){
wid=test;
len=test;
}else{
wid=test;
len=test+1;
if(wid*len<l)
wid++;
}
int a=0;
char arr[][] = new char[wid][len];
for(int i=0;i<wid;i++){
for(int j=0;j<len;j++){
if(a==s.length())
arr[i][j]=' ';
else
arr[i][j]=s.charAt(a++);

}
if(a==s.length())
break;
}
String temp="";
boolean go=false;
for(int i=0;i<len;i++){
for(int j=0;j<wid;j++){
if(!(arr[j][i]==' ')){
temp=temp+arr[j][i];
go=true;
}
}
if(go)
temp=temp+" ";
go=false;
}
System.out.println(temp);
}

}```

## Encryption HackerRank Solution in Python

```from math import sqrt, ceil

message = raw_input().strip()

size = len(message)

t1 = int(ceil(sqrt(size)))
t2 = int(ceil(float(size)/t1))

rows = min(t1, t2)
cols = max(t1, t2)

cipher = []

while message:
cipher.append(message[:cols])
message = message[cols:]

for j in xrange(cols):
tmp = []
for i in xrange(rows):
try:
tmp.append(cipher[i][j])
except IndexError:
pass
print ''.join(tmp),```

## Encryption HackerRank Solution in C#

```using System;

namespace contest
{
class Program
{
static void Main(string[] args)
{
int length = line.Length;
int row = (int) Math.Sqrt(length);
int col = length/row;
if (row * col < length) col++;

while(col-row>1)
{
col--;
row++;
}

string output = "";
for(int i =0; i < col; i++)
{
for (int j = 0; j < row; j++)
{
if (j * col + i < length)
output += line[j*col + i];
}
output += " ";
}

Console.WriteLine(output);
}
}
}```

Attempt Encryption HackerRank Challenge

Next HackerRank Challenge Solution