# Thread: Please i need someone to explain this code step by step

1. ## Please i need someone to explain this code step by step

Hello all I'm new to c language programming can someone explain to me the below code,
I know that it is a playfair encryption in c language. But I need step by step explanation of the code please

----------------------------------------------------------------

Code:
```include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define Matrix 5
int ch;

void pfemulator(char c1, char c2, char key[Matrix][Matrix]) {

int i, j, w, x, y, z;
for (i = 0; i < Matrix; i++) {
for (j = 0; j < Matrix; j++) {
if (c1 == key[i][j]) {
w = i;
x = j;
} else if (c2 == key[i][j]) {
y = i;
z = j;
}
}
}

if (w == y) {
if(ch==1){
x = (x + 1) % 5;
z = (z + 1) % 5;
}
else{
x = ((x - 1) % 5+5)%5;
z = ((z - 1) % 5+5)%5;
}
printf("%c%c", key[w][x], key[y][z]);
} else if (x == z) {
if(ch==1){
w = (w + 1) % 5;
y = (y + 1) % 5;
}
else{
w = ((w - 1) % 5+5)%5;
y = ((y - 1) % 5+5)%5;
}
printf("%c%c", key[w][x], key[y][z]);
}
else {
printf("%c%c", key[w][z], key[y][x]);
}

}

void removeDuplicates(char str[]){
int hash[256]        =  {0};
int currentIndex     = 0;
int lastUniqueIndex  = 0;
while(*(str+currentIndex)){
char temp = *(str+currentIndex);
if(0 == hash[temp]){
hash[temp] = 1;
*(str+lastUniqueIndex) = temp;
lastUniqueIndex++;
}

currentIndex++;
}
*(str+lastUniqueIndex) = '\0';

}

void main() {
int i, j, k = 0, l, m = 0, n;
char key[Matrix][Matrix], keyminus[25], keystr[10], str[25] = {
0
};

char alpa[] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
};

printf("\n ======= Playfair Cipher emulator =======\n");
printf("\n1.Encryption\n2.Decryption\n\nSelect an option (1 or 2): ");
scanf("%d",&ch);
if(ch!=1 && ch!=2){ printf("Invalid Selection"); return;}
// if the user enters wrong choice(1-2) the program displays an error message
fflush(stdin);
printf("\nEnter the key: ");
gets(keystr);
printf("Enter the plain/encrypted text: ");
gets(str);
removeDuplicates(keystr);
n = strlen(keystr);

for (i = 0; i < n; i++) {
if (keystr[i] == 'j') keystr[i] = 'i';
else if (keystr[i] == 'J') keystr[i] = 'I';
keystr[i] = toupper(keystr[i]);
}

for (i = 0; i < strlen(str); i++) {
if (str[i] == 'j') str[i] = 'i';
else if (str[i] == 'J') str[i] = 'I';
str[i] = toupper(str[i]);
}

j = 0;
for (i = 0; i < 26; i++) {
for (k = 0; k < n; k++) {
if (keystr[k] == alpa[i]) break;
else if (alpa[i] == 'J') break;
}
if (k == n) {
keyminus[j] = alpa[i];
j++;
}
}

k = 0;
for (i = 0; i < Matrix; i++) {
for (j = 0; j < Matrix; j++) {
if (k < n) {
key[i][j] = keystr[k];
k++;
} else {
key[i][j] = keyminus[m];
m++;
}
printf("%c ", key[i][j]);
}
printf("\n");
}

printf("\nEntered text :%s\nOutput Text :", str);
for (i = 0; i < strlen(str); i++) {
if (str[i] == 'J') str[i] = 'I';
if (str[i + 1] == '\0') pfemulator(str[i], 'X', key);
else {
if (str[i + 1] == 'J') str[i + 1] = 'I';
if (str[i] == str[i + 1]) pfemulator(str[i], 'X', key);
else {
pfemulator(str[i], str[i + 1], key);
i++;
}
}
}

if(ch==2) printf(" \n\n(Remove unnecessary X if found)\n");
}```

2. > void main()
...
> fflush(stdin);
...
> gets(keystr);
My first thought would be to find someone not stuck with a 1980's DOS mentality to copy/steal code from.

> I know that it is a playfair encryption in c language
Gee - yah think!?
printf("\n ======= Playfair Cipher emulator =======\n");

> But I need step by step explanation of the code please
Sure, do you know about the goldilocks problem?

That it's a waste of our time busting a gut to give you what you ask, when there's a 66% chance of you responding with "too easy" or "too hard".

You show us what you know / have doubts about / don't know a thing and then we'll fill in the blanks.

> Hello all I'm new to c language programming can someone explain to me the below code
Yeah, that code isn't really newbie code material.
What else is going on?