When I run the code, it goes until it prompts me for a string and then stops. I am at a bit of an impasse on what to do next.

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
 if (argc !=2)
    printf("try again");
    return 1;

else {
int k = atoi(argv[1]);

 string s;
printf("write something");
s = GetString();
for (int n = 0; n > strlen(s); n++)
   int l = argv[2][n];

    if (isalpha(n))
         char c = l + k; 
      printf("%c", c);


 return 0;

Take a look at this line:

for (int n = 0; n > strlen(s); n++)

It initializes n, then checks if n > strlen(s) and skips "for" block. Obviously, the condition must be this: n < strlen(s).

Also, it can be rewritten this way:

for (int n = 0, len = strlen(s); n < len; n++)

It is better because function strlen won't be called on every iteration.

  • It now returns "Segmentation Fault" after making those changes
    – adjcode
    Oct 5 '16 at 21:15
  • Yes, indeed. But I believe that having argc == 2, there is no argv[2] in your program. Only argv[0] and argv[1] are available.
    – paul
    Oct 5 '16 at 21:31

You must log in to answer this question.

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