-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.c
63 lines (51 loc) · 1.25 KB
/
encryption.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Encryption
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void encryption( char *str ) {
int len = strlen(str), len2 = 0, i, j, k, rows, cols;
/* Finding the length of the string without spaces */
for( i = 0; i < len; i++ ) {
if( str[i] != ' ' && str[i] != '\n')
len2++;
}
rows = floor(sqrt(len2));
cols = ceil(sqrt(len2));
/* If (rows * cols) is less than length of the string */
if( (rows * cols) < len2 )
rows++;
/* Declaring 2D array for storing the string */
int **arr = (int**)malloc( rows * sizeof(int*));
for(i = 0; i < rows; i++)
arr[i] = (int*)malloc( cols * sizeof(int));
/* Filling the 2D array */
k = 0;
for( i = 0; i < rows; i++ ) {
for( j = 0; j < cols; j++ ) {
/* Ignoring spaces and newline */
if( str[k] != ' ' && str[k] != '\n')
arr[i][j] = str[k];
else
arr[i][j] = str[++k];
k++;
}
}
/* Printing the encrypted array */
for( i = 0; i < cols; i++ ) {
for( j = 0; j < rows; j++ ) {
if( arr[j][i] >= 97 && arr[j][i] <= 122 )
printf("%c", arr[j][i]);
}
printf(" ");
}
return;
}
int main(int argc, char const *argv[])
{
char *str = (char*)malloc( 85 * sizeof( char ));
printf("Enter the string:");
fgets(str, 85, stdin);
encryption( str );
return 0;
}