Home / C / How to do Message Encoding in C Program using Matrix Multiplication?

How to do Message Encoding in C Program using Matrix Multiplication?

Now, if you want to do message encoding in C program and using Matrix multiplication, then here is the C program for performing message encoding. Use this source code and output example to perform message encoding in C program.

Message Encoding in C Program

Source Code

/*
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;
}

 

Also See:   C program for Kaprekar number

Output

$ 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.

So using this C program code for message encoding using Matrix multiplication, you can do message encoding in a c program easily.

Check Also

Restrict Mouse Pointer Position C Graphics Program

This C Program will restrict the Mouse Pointer location or to a particular position or x,y ...

Leave a Reply

Your email address will not be published. Required fields are marked *