1. (Main question- need help on this for code to work! highlighted in screenshot.) My PSET2 code works fine up until asking the user to input plaintext, but becomes funky and outputs blank spaces as the ciphertext. Is there anything wrong with the printf format/enciphering formula?

  2. (more optional) I want to print an error message if the user does not input a valid alphabetic word into argv1 reading Invalid Keyword, but this message currently prints as many times as the number of invalid characters rather than just once. I believe this is because printf("Invalid Keyword") is inside the for loop, but I haven't been able to successfully place it outside the for loop as it needs the variable 'a' of the loop to work properly, also it isn't logical to place it outside !isalpha. I just can't figure out how to properly declare it outside the for loop.

  3. (more optional) On a similar note to 2, my variable m and a seem to be the same thing, but again can't really figure out how best to declare it just once outside globally as I believe I would still need to write an initialisaiton, condition, and increment for their respective for loops. Again, I'm sure it would work if I declare them both, but maybe if I can figure out a way to not do so if not necessary I can shorten my code.

Thank you so much for any help in advance, I highly appreciate it!

whole code except headers

1 Answer 1

  1. With the code shown, you never get to the state where you ask for plaintext (see 2.). But if you did, note that you would have to map keyword letters 'a' and 'A' to a shift of 0, 'b' and 'B' to a shift of 1, and so on, you cannot just add the letter. Also, you don't seem to shift back into the letter range. Have you done caesar first? They only differ in the key part, not in the plaintext part.

  2. You need curly braces if you want to have more than one statement executed conditionally. Your code without would say

    for (int a = 0; a < strlen(argv[1]); a++)
        if (!isalpha(argv[1][a]))
            printf("Invalid keyword"); // I would add a \n here
    return 1; // executed always, not conditionally
  3. Having nested loops makes no sense, a single one will do. You need to print exactly one character for each plaintext character. You need a second index, though, incremented whenever you encrypt a letter, and wrapped around on reaching the keyword's end (can be achieved with the % operator).

You must log in to answer this question.

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