0

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

Barmar
  • 741,623
  • 53
  • 500
  • 612
Chris04
  • 11
  • 2
  • Never ***ever*** use `gets`! It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) it has even been removed from the C language. Use e.g. `fgets` instead. – Some programmer dude Nov 17 '22 at 16:16
  • 2
    As for your problem, shouldn't `h=strtok(str,"-");` really be `h=strtok(NULL,"-");`? – Some programmer dude Nov 17 '22 at 16:17
  • I just want to make a while loop to check if the encrypted word has any errors and if not, then a while loop for it to write the word in english, if the encrypted word is correctly written. If i do that, Wont p change in the first whie loop? – Chris04 Nov 17 '22 at 16:33

1 Answers1

0
    p=strtok(str,"-");
    h=strtok(str,"-");

This cannot work. strtok modifies the input string and can only be used once on a given input.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113