1
#include <stdio.h>
#include <string.h>

int main()
{
    
    char cipher[5][5] = {
                    {'a', 'b', 'c', 'd', 'e'},
                    {'f', 'g', 'h', 'i', 'k'},
                    {'l', 'm', 'n', 'o', 'p'},
                    {'q', 'r', 's', 't', 'u'},
                    {'v', 'w', 'x', 'y', 'z'}
                        };
                    
    
    char cm;
    char *original;
    char *portion;
    printf("Enter ciphered message: ");
    scanf("%s", &cm);


    original = strdup(&cm);
    portion = strtok(original, "-");

    
    while (portion != NULL){

        int i = portion[0]-'0';
        int j = portion[1]-'0';

        printf("%c", cipher[i][j]);
        portion = strtok(NULL, "-");
        
        }
    

    return 0;
}

Hello, I'm a new computer science student and I'm already having a problem. I'm writing a polybius cypher and I can't seem to get the user input saved correctly to use at strdup

With input i.e.: 00-11-22-33-44 I should receive "agntz" but I can't get it to print. I'm new here so I apologize if I haven't formatted my question correctly.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 5
    `scanf("%s", &cm);` - you are trying to read a string into a single character variable. – Eugene Sh. Nov 09 '22 at 21:39
  • 3
    @CompuerScienceNoob, Try `char cm;.... scanf("%s", &cm);` --> `char cm[100]; .... scanf("%99s", cm);`. – chux - Reinstate Monica Nov 09 '22 at 21:44
  • Consider not using `strtok()`. It doesn't look like you particularly need it, and if you don't use it then it doesn't look like you would have any need for `strdup()` either. Not that I see the point of the `strdup()` itself in the code presented. If indeed you don't have any further use for the ciphertext after you've deciphered it, then probably you don't need to read the whole thing in one go in the first place - you ought to be able to process the input 1-2 characters at a time. – John Bollinger Nov 09 '22 at 22:22

1 Answers1

0

The main issue is that you char cm is a single character but you want to read a string char *cm. If your scanf() supports the optional m character then the easiest option to have it allocate your string for you:

#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <string.h>

int main(void) {
    char cipher[5][5] = {
        {'a', 'b', 'c', 'd', 'e'},
        {'f', 'g', 'h', 'i', 'k'},
        {'l', 'm', 'n', 'o', 'p'},
        {'q', 'r', 's', 't', 'u'},
        {'v', 'w', 'x', 'y', 'z'}
    };
    char *cm;
    char *original;
    char *portion;
    printf("Enter ciphered message: ");
    scanf("%ms", &cm);
    original = strdup(cm);
    portion = strtok(original, "-");
    while (portion != NULL){
        int i = portion[0]-'0';
        int j = portion[1]-'0';
        printf("%c", cipher[i][j]);
        portion = strtok(NULL, "-");
    }
    printf("\n");
}

and the you are now getting the expected output:

Enter ciphered message: 00-11-22-33-44
agntz

It would be a good idea to check that portion[0] and portion[1] are indeed numbers (for instance by usingisdigit()) to avoid out of bound access of cipher. Also check that you only get two digits in portion (strlen(portion) == 2).

Is missing j in the cipher on purpose?

You can refactor the above along these lines:

#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <string.h>

int main(void) {
    char cipher[] = "abcdefghiklmnopqrstuvwxyz";
    char *cm;
    printf("Enter ciphered message: ");
    scanf("%ms", &cm);
    for(char *portion = strtok(cm, "-"); portion; portion = strtok(NULL, "-")) {
        int i = portion[0]-'0';
        int j = portion[1]-'0';
        printf("%c", cipher[5*i+j]);
    }
    printf("\n");
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38