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

int main(int argc, string argv[])

{
 int k =0;
 if (argc != 2)
 {
  printf("Sorry!");
 return 1;
 }

  k = atoi(argv[1]);

    int n=0;
   string s = GetString();
   for(int i = 0 ; i < strlen(s);i++)
   {

     int l =  (s[i]+k);

    if ( l >122)
    {
     printf("%c",  (char) l-26 );
    }

    if ( l == 32 + k)
    {
    printf("%c",(char) l- k);
    }

     if( l >= 97 && l <= 122 )
     {
     printf("%c" , (char) l);
     }

     if (s[i]>='A'&& s[i]<='Z')
     {
      n = s[i]+k;

      if( n > 90)
      {
       printf("%c",(char) n - 26);
      }

      if( n>=65 && n <=90)
      {
       printf("%c",(char) n);
      }
     }

     if(s[i]>='1' && s[i]<='5')
     {
      printf("%c",(char) s[i]);
     }

    if(s[i]>=33 && s[i]<=47)
    {
     printf("%c",s[i]);
    }
     if ( l >= 148 && l <= 174)
     {
     printf("%c",  (char) l-52 );
    }
    if ( l>=174 && l<=200)
    {
     printf("%c",  (char) l-78 );
    }

  }
       printf("\n");


}

result:

:) caesar.c exists
:) caesar.c compiles
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:( encrypts "barfoo" as "onesbb" using 65 as key
   \ expected output, but not "onesbb\n"
:) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
:) handles lack of argv[1]
0

Sometimes it's what you do not see.....

The interesting thing about that test is that k > 26, ie outside the "alphabet range". Let's take the 'b' in "barfoo" through the first iteration.

b is ascii 97 so int l = (s[i]+k); sets l to 97 + 65 = 163.
l is > 122 so this line printf("%c", (char) l-26 ); prints 163 - 22 = 137 cast to a char. That is "unprintable", chars are in the range 0 - 127.
The program wends its way to here if ( l >= 148 && l <= 174) where it prints 163 - 52 = 111, which is ascii for 'o'.

check50 is detecting that the program outputs unprintable characters, which the eye cannot see since, well, they are unprintable. If you want to "see" what check50 sees, try changing all your print format strings from "%c" to "%c!", then run barfoo/65 through the program.

There is a hint in the spec:

Anyhow, even though there exist only 26 letters in the English alphabet, you may not assume that k will be less than or equal to 26; your program should work for all non-negative integral values of k less than 231 - 26. (In other words, you don’t need to worry if your program eventually breaks if the user chooses a value for k that’s too big or almost too big to fit in an int. Now, even if k is greater than 26, alphabetical characters in your program’s input should remain alphabetical characters in your program’s output. For instance, if k is 27, A should not become [ even though [ is 27 positions away from A in ASCII; A should become B, since 27 modulo 26 is 1, as a computer scientists might say. In other words, values like k = 1 and k = 27 are effectively equivalent.

You must log in to answer this question.

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