0

So i was working with cryptogram in one of my hands-on exercise provided by my professor. My own version of cryptogram is that in encrypt() it should convert the strings into numbers while the decrypt will convert back the encrypt() results into letter. So, I was hoping that in the decrypt part should convert the encrypt() results into letters.

https://onlinegdb.com/MtyVfAcSI

This is what I wrote in decrypt(), this should convert the numbers in alphabet. For example, I input these numbers 75 102 123 106 102 109 thus, it should show the result 'Jeziel'

void decrypt(int numChar[], int random)
    {
     int x = 0;
     char sMessage[21] = {0};
    
     while ( numChar[x] ) {
     sMessage[x] = numChar[x] - random;
     x++;
     } //end loop
     sMessage[x] = '\0';
     printf("\nDecrypted Message is: ");
    //print the decrypted message
     while ( sMessage[x] ) {
     printf("%c", sMessage[x]);
     x++;
     } //end loop
    } //end decrypt function

in the main() I do this instead;

char myString[21] = {0};
 int decryptnum[21] = {0};
 int iSelection = 0;
 int iRand;
 srand(time(NULL));
 iRand = (rand() % 4) + 1; // random #, 1-4

 while ( iSelection != 4 )
    {
        printf("\n\n1\tEncrypt Clear Text\n");
        printf("2\tDecrypt Cipher Text\n");
        printf("3\tGenerate New Key\n");
        printf("4\tQuit\n");
        printf("\nSelect a Cryptography Option: ");
        scanf("%d", &iSelection);

    switch (iSelection) {
    case 1:
        printf("\nEnter one word as clear text to encrypt: ");
        scanf("%s", myString);
        encrypt(myString, iRand);
        break;
     case 2:
    printf("\nEnter cipher text to decrypt: ");
    x = 0;
    while (scanf("%d", &decryptnum[x]) == 1) {
        x++;
    }
    break;
     case 3:
        iRand = (rand() % 4) + 1; // random #, 1-4
        printf("\nNew Key Generated\n");
        break;
            } //end switch
        } //end loop
    } //end main

I almost forgot the encrypt();

    void encrypt(char sMessage[], int random)
{
 int x = 0;
 int numChar[21] = {0};

 while ( sMessage[x] ) {
numChar[x]=sMessage[x] + random;
 x++;
 } //end loop
 x = 0;
 printf("\nEncrypted Message is: ");
//print the encrypted message
 while ( numChar[x] ) {
 printf("%d ", numChar[x]);
 x++;
 } //end loop
} //end encrypt function
jusJeme
  • 1
  • 1
  • Please copy & paste your code directly in your question as a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) instead of providing external links. – MikeCAT Feb 26 '23 at 02:21
  • 1
    `scanf("%d ",decryptnum);` will read only one number. You will have to add more code to read the rest of numbers. – MikeCAT Feb 26 '23 at 02:36
  • 1
    Using `"%d "` as a `scanf` format string is generally wrong. You may want to read this: [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/q/19499060/12149471) – Andreas Wenzel Feb 26 '23 at 02:44
  • so i will use a for loop? – jusJeme Feb 26 '23 at 03:02
  • `75 102 123 106 102 109` for `Jeziel` means `random == 1` because the ASCII values for `Jeziel` are `74 101 122 105 101 108`. Was that your intention? Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal Complete Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Providing the necessary details will allow everyone here to help you with your question. – David C. Rankin Feb 26 '23 at 03:31

0 Answers0