1

thank you for your help in advance! I can't wrap my head around what's wrong with my code as the output is (seemingly) correct. I suspect it has something to do with the null character.

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


int main(int argc, string argv[])
{
    if (argc == 1) //user didn't enter any key
    {
        printf("ERROR: Valid key missing!\n");
        return 1;
    }


    char keyArray[26];
    for (int x = 0; x < 26; x++) //save values of argv[1][x] into new array for later comparison
    {
        keyArray[x] = argv[1][x];
        //printf("%c\n", tolower(keyArray[x]));

    }

    if (argc == 2 && strlen(argv[1]) == 26) //input contains 26 digits
    {
        for (int i = 0; i < 26; i++) //go through (keyArray[i] and examine input
        {
            if (keyArray[i] >= 'a' && keyArray[i] <= 'z')
            {
                i++;
            }
            else if (keyArray[i] >= 'A' && keyArray[i] <= 'Z')
            {
                i++;
            }
            else
            {
                printf("ERROR: Input valid key!\n");
                return 1;
            }

        }

        //if letters occur more than once
        for (int q = 0; q < 26; q++)
        {
            int count = 0; //count has to be IN first for-loop to be resetted to 0 every iteration
            for (int j = 0; j < 26; j++)
            {
                if (tolower(keyArray[q]) == tolower(keyArray[j]))
                {
                    count++;

                    if (count == 2)
                    {
                        printf("ERROR: Use letters only once!\n");
                        return 1;
                    }
                }
            }
        }

        // ######################################################

        string plainText = get_string("plaintext: ");



        char plainArray[strlen(plainText)+1];

        char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

        printf("cipertext: ");


        //save plainText into array
        for (int w = 0; w < strlen(plainText); w++)
        {
            plainArray[w] = plainText[w];
            //printf("array: %c\n", plainArray[w]);
        }
        plainArray[strlen(plainText)] = '\0'; //save null character into last element of array
        //printf("array at 26: %c\n", plainArray[26]);

        // ######################################################

        //encrypt message
        for (int b = 0; b < strlen(plainText); b++)
        {
            if (plainArray[b] >= 'a' && plainArray[b] <= 'z') //input is lower caps letter
            {
                for (int d = 0; d < strlen(alphabet); d++) //go through the alphabet and find out at which position the input is; h = alphabet[7]
                {
                    if (plainArray[b] == alphabet[d])
                    {
                        printf("%c", tolower(keyArray[d]));
                    }
                }
            }

            else if (plainArray[b] >= 'A' && plainArray[b] <= 'Z') //input is upper caps letter
            {
                for (int e = 0; e < strlen(alphabet); e++) //go through the alphabet and find out at which position the input is; h = alphabet[7]
                {
                    if (toupper(plainArray[b]) == toupper(alphabet[e]))
                    {
                        printf("%c", toupper(keyArray[e]));
                    }
                }
            }
            else
            {
                printf("%c", plainArray[b]);
            }

        }
        printf("%c\n", plainArray[strlen(plainText)]); // null character
        return 0;
    }
    else
    {
        printf("ERROR: Input valid key!\n");
        return 1;
    }

}

These are the errors I'm receiving: enter image description here

enter image description here

Thank you guys, Jas

1

You're correct. The system is likely tripping up because you are providing an extra null character to the output. Try again without this line: printf("%c\n", plainArray[strlen(plainText)]);

p.s. There is actually no need for the null character at all in this program. Null is used to tell the computer to stop parsing through a list of characters, but your program doesn't require that of the computer. The computer always knows when to stop thanks to the for() loops.

Edit: That removes the newline character needed in the answer. The print null line has to be replaced with just printing the newline character: printf("\n");

5
  • 1
    Thank you for your quick reply! This unfortunately doesn't resolve my errors. I still get: " expected "ciphertext: Z\...", not "cipertext: Z" " – mylifeasjas Mar 28 '20 at 13:54
  • Oh deleting that line removed the \n character needed in the output. Just add the line back but without the null character and only the newline character. I'll edit the answer post. – TLH Mar 28 '20 at 13:57
  • Nope sorry, replacing it with just printf("\n"); gives me: expected "ciphertext: Z\...", not "cipertext: Z\n..." – mylifeasjas Mar 28 '20 at 17:27
  • You also spelled ciphertext wrong. Maybe that should fix it? – TLH Mar 29 '20 at 6:09
  • i am screaming. this was actually the problem! i am astonished how oblivious one can be to misspelled words. thank you so much for your help! – mylifeasjas Mar 29 '20 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .