Really having a hard time doing this caesar problem my cipher text is not taking spaces to account and when I type in ./caesar by itself it gives me a message like this:

caesar.c:10:18: runtime error: null pointer passed as argument 1, which is declared to never be null /usr/include/stdlib.h:148:33: note: nonnull attribute specified here Segmentation fault

I don't know what else I'm doing wrong is it possibly my first if else loop?

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

int main(int argc, string argv[])
// establish conditions for key which is k also get unencrypted string
int k = atoi(argv[1]);
if(argc==2 && k)
    string s = get_string();
    printf("plaintext:%s\n", s);

//check each individual character of plaintext and preserve characters
    for(int i = 0, n = strlen(s); i < n; i++)
        if(isalpha(s[i]), isupper(s[i])) 
            // convert asci to alphbetical for upper case
            int cipher = s[i]-65;
            int c = (cipher + k) % 26;
            printf("%c", c+65);
        //convert for lower case
        else if(islower (s[i]), isalpha(s[i]))
            int cipher2 = s[i]-97;
            int d = (cipher2 + k) % 26;
            printf("%c", d+97);
    return 0;
    printf("Usage: ./caesar k\n");
    return 1;

When you do not supply an argument, the runtime error comes here int k = atoi(argv[1]); because argv[1] is null and atoi cannot accept null. You need to check that the user has supplied an argument before you try to process that argument. argc can help.

This if(isalpha(s[i]), isupper(s[i])) is not familiar C notation. I don't know how it will be evaluated. The more conventional notation would be if(isalpha(s[i]) && isupper(s[i])).

If s[i] is a space (or any other "non-alpha" character) then isalpha(s[i]) && isupper(s[i]) is false and islower (s[i]) && isalpha(s[i]) is false. In that case the program will output (printf)....nothing, it won't printf at all. So any non-alpha characters will be "lost".

| improve this answer | |
  • Awesome I believe you helped me figure out the bottom two things but I am still having trouble about int k = atoi(argv[1]). Does that mean I have to create a Null line? – Roundabout Jan 24 '17 at 5:32
  • 1
    As mentioned above by @DinoCoderSaurus, try first checking for argc usage (if argc != 2) and only then converting the string to an int – Gordon Jan 24 '17 at 11:45

You must log in to answer this question.

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