Ok so i have the basic idea of caesar and heres what i have so far:

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

int main(int argc, string argv[]){
int k = atoi(argv[1]);
string toEncrypt;

if(argc != 2 || k < 0){

    printf("k cant be negative and you must enter a command line integer");

    return 1;


printf("Please enter a message to encrypt with your command line integer: ");

toEncrypt = GetString();

for (int i = 0, n = strlen(toEncrypt); i < n; i++){


        int ascii = (int)toEncrypt[i];

        char encrypted = (char)ascii + k;

        printf("%c", encrypted);





But the problem is that for anything that isn't a letter, it just skips it and doesn't print it at all..

I know how to make the letters wrap around after 26 but first i need to get this right. Does anyone have any idea how I can print the non-letter characters or symbols in their right places without changing them?

1 Answer 1


In your for (int i = 0, n = strlen(toEncrypt); i < n; i++), you only have an if statement for isalpha(), which checks whether something is a letter. In other words, there is no if statement for the case of one of the array indexes being a number.

You could add an else statement under the if statement in the for loop, something like:

    char encrypted = toEncrypt[i];

and put printf("%c", encrypted) at the end of the for loop outside of the if or else statements


You must log in to answer this question.

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