The program i want to write reads an encrypted word and translates it. First of all it makes a 5x5 2d array and puts the letters from a to z in the cells, except the letter j. Then it reads a string of pairs that contain 2 numbers at a time with the symbol "-" separating them. (The string is not allowed to have any more than 24 characters ). After the program gets the string, it breaks it so that the pointer gets one pair at a time of the 2 numbers and with the help of atoi, it makes this pair into an int. Then we break the pair and we separate it into 2 different numbers. The first number represents the line of the 2d array in which the letter we want is and the second number represents the pile of the 2d array in which the letter we want is. Then the program gets those coordinates and finds the letter that
corresponds to them in the 2d array. If the user types a number that is more than the 2d's capacity, the program should write back"Out of bounds" and if the user types any letter instead of a number, it should write back "Unable to decode".
Example of an encrypted word:
12-04-20-20-23 = Hello
The 2d array we create:
X 0 1 2 3 4
0 a b c d e
1 f g h i k
2 l m n o p
3 q r s t u
4 v w x y z
Sorry about my English, i tried my best to explain it.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
int i,j;
char k='a', arr[5][5];
for (i=0; i<=4; i++)
{
for (j=0; j<=4; j++)
{
arr[i][j]= k;
k= ++ k;
if(k=='j')
k= ++k;
}
}
char str[74], *p, *h;
gets(str);
p=strtok(str,"-");
h=strtok(str,"-");
while(p!=NULL)
{
if(atoi(p)/10>4||atoi(p)%10>4)
{
printf("Out of bounds");
return 0;
}
else if (isalpha(*p))
{
printf("Unable to decode");
return 0;
}
p=strtok(NULL,"-");
}
while(h!=NULL)
{
printf("%c", arr[atoi(h)/10][atoi(h)%10]);
h=strtok(NULL,"-");
}
return 0;
}
For example:
12-04-20-20-23
must give back 'hello'
Instead it gives back 'h'
12-61
must give back: "Out of bounds"
Gives back h