Home > Back-end >  I am trying to print out array of characters but it does not print out the first letter
I am trying to print out array of characters but it does not print out the first letter

Time:11-20

#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

CodePudding user response:

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 
  •  Tags:  
  • c
  • Related