Users Online

· Guests Online: 34

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C Program to Perform Encoding of a Message Using Matrix Multiplication

/*
Restriction: Input string has to be in lower case without any special characters
*/
#include <stdio.h>
#include <string.h>


void mul(int first[3][3], int second[3][10], int result[3][10])
{
int c, d, sum, k;
int i, j;
for ( c = 0 ; c < 3 ; c++ )
{
for ( d = 0 ; d < 10 ; d++ )
{
sum = 0;
for ( k = 0 ; k < 3 ; k++ )
{
sum = sum + first[c][k] * second[k][d];
}
result[c][d] = sum;
}
}

}
int main()
{
char str[29] = "this message is to be encoded";
int len;
int i, j;
int result[3][10] = {0};
int key[3][3] = {
{-3, -3, -4},
{0, 1, 1},
{4, 3, 4}
};
int inv_key[3][3] = {
{1, 0, 1},
{4, 4, 3},
{-4, -3, -3}
};
int encode[3][10] = {32};
int decode[3][10] = {0};
len = strlen(str);

for (i = 0; i < 10; i++)
{
for(j = 0; j < 3; j++)
{
if (str[j + i*3] >='a' && str[j + i*3] <='z')
{
encode[j][i] = str[j + i*3] - 96;

}
if (str[j + i*3] == 32)
{
encode[j][i] = 32;
}
if (str[j + i*3] == '\0')
break;
}
if (str[j + i*3] == '\0')
break;
}
mul( key, encode, result);
printf("\nEncoded message to be sent: ");
for (i = 0; i < 10; i++)
{
for ( j = 0 ; j < 3; j++)
printf("%d, ", result[j][i]);
}
printf("\nDecoded message is: ");
mul(inv_key, result, decode);
for (i = 0; i < 10; i++)
{
for ( j = 0; j < 3; j++)
{
if ( ((decode[j][i]+96)) >= 97 && ((decode[j][i]+96) <= 123))
printf("%c", (decode[j][i] + 96) );
else if ( decode[j][i] == 32)
printf(" ");
else
return;
}
}
return 0;
}

$ gcc encode.c -o encode
$ ./encode

Encoded message to be sent: -120, 17, 140, -205, 45, 224, -148, 38, 153, -44, 12, 45, -199, 28, 231, -216, 35, 248, -122, 7, 154, -167, 19, 199, -70, 19, 73, -27, 4, 32,
Decoded message is: this message is to be encoded

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.10 seconds
10,840,929 unique visits