0
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void encryptmessage1(char[][6]);
void encryptmessage2(char[][6]);
int
main()
{
    char array[5][6] = {
        {'A', 'B', 'C', 'D', 'E'},
        {'F', 'G', 'H', 'I', 'J'},
        {'K', 'L', 'M', 'N', 'O'},
        {'P', 'Q', 'Z', 'R', 'S', 'T'},
        {'U', 'V', 'W', 'X', 'Y'}
    };
    int choice;

    printf("********************************\n");
    printf("**ENCRYPTION/DECRYPTION SYSTEM**\n");
    printf("********************************\n");
    printf("\n");
    printf("1.Code the message\n");
    printf("2.Decode the message\n");
    printf("3.EXIT\n");
    printf("Make your choice: ");
    scanf("%d", &choice);

    switch (choice) {
    case 1:
        encryptmessage1(array);
        encryptmessage2(array);
        break;

    case 2:
        // decrypt message
        break;

    default:
        break;
    }

    return 0;
}

void
encryptmessage1(char array[][6])
{
    int i, j, loop, k = 0, row, col, len = 0;
    char str[80] = {};
    char temparr[80] = {};                   // temporary array
    char temp;

    printf("Please enter your message : ");
    temp = getchar();

    // reads string
    while ((str[k] = getchar()) != '\n') {
        k++;
    }
    i = 0;

    // loop to temporary store values from another array
    for (loop = 0; loop < 80; loop++) {
        temparr[loop] = str[loop];
    }

    // Calculating length of the array
    len = sizeof(str) / sizeof(str[0]);
    // Checks for space character in array if its there then ignores it
    // and swap str[i] to str[i+1];
    for (i = 0; i < len; i++) {
        if (str[i] == ' ') {
            for (j = i; j < len; j++) {
                str[j] = str[j + 1];
            }
            len--;
        }
    }

    i = 0;
    // from lowercase to uppercase
    while (str[i] != '\n') {
        if (islower(str[i])) {
            str[i] = toupper(str[i]);
        }
        i++;

    }
    puts(str);

    i = 0;
    k = 0;
    while (str[k] != '\n') {
        for (row = 0; row < 5; row++) {
            for (col = 0; col < 6; col++) {
                if (str[k] == array[row][col]) {

                    temparr[i] = '0' + row;
                    temparr[i + 1] = '0' + col;
                    i += 2;
                }

            }
        }
        k++;
    }
   puts(temparr);
   }


void
encryptmessage2(char array[][6])
{ int i, j, loop, k =0, row, col;
    char key[80] = {};
    char temparr2[80] = {};                   // temporary array
    char temp;

    printf("Please enter your key : ");
    temp = getchar();

// reads string
    while ((key[k] = getchar()) != '\n') {
        k++;
    }
    i = 0;

// loop to temporary store values from another array
    for (loop = 0; loop < 80; loop++) {
        temparr2[loop] = key[loop];
    }

// array from lowercase to uppercase
    while (key[i] != '\n') {
        if (islower(key[i])) {
            key[i] = toupper(key[i]);
        }
        i++;

    }

//Printing the array with spaces using pointer
char *ptr = key;
if (*ptr) {
    putchar(*ptr++);
    while (*ptr) {
        putchar(' ');
        putchar(*ptr++);
    }
}

}

[outputoftheproblem](https://i.stack.imgur.com/wLUGa.png)

If I type in the keyword cortina it just prints out ortina without the first letter I dont know if the problem is with the pointer Also any ideas how can I print out the same set of numbers which were encrypted from str[] as an array under the 'CORTINA' word like the example above:

C O R T I N A
1 1 0 0 3 4 0
3 4 5 6 3 2 5
2 3 4 5 6 7 8 
2 3 4 3 2 4 5
Barmar
  • 741,623
  • 53
  • 500
  • 612
XIz
  • 1
  • 4
  • The first character is put into `temp` with `temp = getchar();`. You never use that, what is it for? – Barmar Nov 18 '22 at 23:34
  • thats the only way i can read the array for some reason if i try putting str[]=getchar() it wont work or without the getchar it doesnt let the user enter the message – XIz Nov 18 '22 at 23:41
  • Are you trying to skip over the newline that was left in the input stream after the `scanf()`? – Barmar Nov 18 '22 at 23:43
  • the problem is that you call `encryptmessage1()` after the `scanf()`, so you need to skip the newline. But `encryptmessage2()` is called after `encryptmessage1()`, which doesn't leave the newline. – Barmar Nov 18 '22 at 23:45
  • 1
    I suggest you change all your code to use `fgets()` to read a line of input everywhere. Then you can use `sscanf()` to extract the option in the main code, and loop over it in the encryption code. Mixing `scanf()` and `getchar()` is error-prone like this. – Barmar Nov 18 '22 at 23:46
  • @Barmar I have removed getchar() from the code and replaced it with fgets() ,it does let me input the characters but the problem with printing the keyword without its first character is still there. – XIz Nov 19 '22 at 00:03
  • You need to add a space after the %d in main() so it clears the newline from your menu choice. Or use fgets there, too. – Allan Wind Nov 19 '22 at 00:05
  • You need to use `fgets()` in `main()` as well. Otherwise `fgets()` will read the empty line after the number. See https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf – Barmar Nov 19 '22 at 00:06
  • `str[j] = str[j + 1]` is out of out bounds. – Allan Wind Nov 19 '22 at 00:06
  • `char temparr[80] = {}` is not standard c, either use `{ 0 }` or just leave it uninitialized. – Allan Wind Nov 19 '22 at 00:15
  • @Barmar isnt fgets() used for a string or a text line, how am I going to be able to change the scanf in main() when 'option' is an integer – XIz Nov 19 '22 at 00:28
  • Yes. You change it like this `char choice[3];` and then `if (*choice == '1')` – Allan Wind Nov 19 '22 at 00:29
  • You use `fgets(stdin, buffer, sizeof buffer)` to read the line, then `sscanf(buffer, "%d", &choice)` to get the choice. Or `choice = atoi(buffer);` – Barmar Nov 19 '22 at 01:50

1 Answers1

0

I fixed the missing first character and out of bounds access. Refactored code reduce duplication and minimize scope of variables.

The key is printing as requested but it's not clear how you want to align the key and cipher text. You are printing ciphertext before the key is read, and you are not using the key for anything in your code. I assume you want to scramble the array using the key.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define STR_LEN 80

void encode(const char array[][6], const char *plaintext, char *ciphertext);
void encrypt(const char array[][6]);
int lookup(const char array[][6], char c, size_t *row, size_t *col);
void strip(char *str);
void upcase(char *str);

void encode(const char array[][6], const char *plaintext, char *ciphertext) {
    size_t i = 0;
    for(; plaintext[i]; i++) {
        size_t row;
        size_t col;
        if(lookup(array, plaintext[i], &row, &col)) {
            ciphertext[2 * i] = '0' + row;
            ciphertext[2 * i + 1] = '0' + col;
        } else {
            ciphertext[2 * i] = ' ';
            ciphertext[2 * i + 1] = ' ';
        }
    }
    ciphertext[2*i] = '\0';
    puts(ciphertext);
}

void encrypt(const char array[][6]) {
    printf("Please enter your message : ");
    char plaintext[STR_LEN];
    fgets(plaintext, sizeof plaintext, stdin);
    plaintext[strcspn(plaintext, "\n")] = '\0';

    strip(plaintext);
    upcase(plaintext);
    puts(plaintext);
    char ciphertext[2 * STR_LEN - 1];
    encode(array, plaintext, ciphertext);

    printf("Please enter your key : ");
    char key[STR_LEN];
    fgets(key, sizeof key, stdin);
    key[strcspn(key, "\n")] = '\0';
    upcase(key);

    for(size_t i = 0; key[i]; i++) {
        putchar(key[i]);
        putchar(' ');
    }
    putchar('\n');
}

int lookup(const char array[][6], char c, size_t *row, size_t *col) {
    for(*row = 0; *row < 5; (*row)++)
        for(*col = 0; *col < 6; (*col)++)
            if(c == array[*row][*col])
                return 1;
    return 0;
}

void strip(char *str) {
    size_t i = 0;
    for(size_t j = 0; str[j];) {
        if(str[j] == ' ')
            j++;
        else
            str[i++] = str[j++];
    }
    str[i] = '\0';
}

void upcase(char *str) {
    while(*str) {
        *str = toupper(*str);
        str++;
    }
}

int main(void ) {
    printf(
        "********************************\n"
        "**ENCRYPTION/DECRYPTION SYSTEM**\n"
        "********************************\n"
        "1. Code the message\n"
        "2. Decode the message\n"
        "3. EXIT\n"
        "Make your choice: "
    );
    char choice[3];
    fgets(choice, 3, stdin);
    if(*choice == '1') {
        const char array[5][6] = {
            {'A', 'B', 'C', 'D', 'E'},
            {'F', 'G', 'H', 'I', 'J'},
            {'K', 'L', 'M', 'N', 'O'},
            {'P', 'Q', 'Z', 'R', 'S', 'T'},
            {'U', 'V', 'W', 'X', 'Y'}
        };
        encrypt(array);
    }
}

Here is the sample run:

********************************
**ENCRYPTION/DECRYPTION SYSTEM**
********************************
1. Code the message
2. Decode the message
3. EXIT
Make your choice: 1   
Please enter your message : test
TEST
35043435
Please enter your key : cortina
C O R T I N A 
Allan Wind
  • 23,068
  • 5
  • 28
  • 38