I've tried so hard for a week to solve this and just can't - despite some attempts from gitter to help me. I've followed some advice to index the keyword before the loop and only use one loop to encipher but I cannot get this code to work. Can anyone please help point me in the right direction?? It passes the check50 checks for all the peripheral stuff but just won't actually encipher the plaintext...Thank you!

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

int main(int argc, string argv[])
    //define result
    int result;

    //confirm command line argument is one word only, reprompt if not.
    if (argc != 2) {
        printf("You did not enter one keyword as a command line argument, please start again");
        return 1;

    //confirm command line argument is alphabetical not numeric, reprompt if not.
     for (int j = 0, x = strlen(argv[1]); j < x; j++) {   
                int key = argv[1][j];

                    if (isdigit(key)) { 
                    printf("You did not enter an alphabetic keyword");
                    return 1;

            //prompt for plaintext
            printf("Enter a string of plaintext for encryption: ");
            string s = get_string();
            if (s != NULL) {

                //initialise index and convert keyletter to lower case then to a = 0 etc
                int y = 0;
                int k = argv[1][y];
                k = tolower(k);
                k = k - 'a';   

               //loop through plaintext
                for (int i = 0, n = strlen(s); i < n; i++)
                    int letter = s[i];

                    //encipher plaintext letter/punctuation etc and print ciphered letter
                    if (isalpha (letter) && islower(letter)) {
                            result = ((letter - 'a') + k) % 26;
                            result = (result + 'a');


                    else if (isalpha (letter) && isupper(letter)) {
                            result = ((letter - 'A') + (k-32)) % 26;
                            result = (result + 'A');

                    else {
                        result = letter;


                    printf("%c", result);

                        // iterate through keyletters returning to the beginning if plaintext is longer than key
                        if (y == strlen(argv[1]))
                        y = 0;

            // print final newline    
            return 0;

  • Maybe you need another k = tolower(argv[1][y]) - 'a'; inside your loop? Also, what's k-32 meant to be?
    – Blauelf
    Dec 5, 2016 at 13:18
  • And better not use isdigit(key) here, but !isalpha(key), otherwise $!?. would be considered a valid keyword, as it contains no digits.
    – Blauelf
    Dec 5, 2016 at 13:26
  • Thanks (again!) Blauelf! WOW! I put another declaration in the loop and it complained so I just moved the declaration and deleted the k-32 (I was overthinking - I thought I had to modify it to an uppercase key for uppercase text but realise I didn't...) and now it's doing everything right except for the word 'say' in the 'world, say hello!' encryption....how on earth is it doing that....:-( still, I"m so much closer than I was so THANK YOU for the nudge!! I'll come back to it later as I have a meeting now, but any clues to why that's happening would be very much appreciated...
    – CallyB
    Dec 5, 2016 at 13:35
  • Just saw your other comment - I'll try that in case it's causing the problem...thanks - I wasn't sure if putting the ! in front of isalpha was ok in c so that's the only reason I'd used the isdigit...and I didn't want to copy once I'd seen you post it in gitter, but it's only one word...so I will!
    – CallyB
    Dec 5, 2016 at 13:36
  • nope - I'm still getting 'szz' instead of 'rby' in that world say hello test....what the...??! Will work on it later...!
    – CallyB
    Dec 5, 2016 at 13:38

1 Answer 1


I got it!!!!! With thanks to Blauelf for giving me endless clues, I realised the reason that the world, say hello! test wasn't working is that I was applying the key to the non-letters and once I moved the iteration of the key to the if and else if blocks it worked!! The other solves were to not deduct 32 from the key in the uppercase block (never needed!) and move the declaration of the key letter into the loop....phew!

You must log in to answer this question.

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