0

check50 :( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key \ expected output, but not "ciphertext: iadxpemktqxxa\n"

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



int main (int argc, string argv[])

{ //Start of code

    // print erro 
    if (argc != 2)

    {
      printf("missing command-line arugment");
      return 1;
    }

    {
      // get user input
      printf ("plaintext: ");
    } 

     //convent k from string to int
      int k = atoi(argv[1]);
      string p = get_string();
      if (p != NULL)

      {
      printf ("ciphertext: \n");
      }

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

      //check for alphabetic letter and upper cap
      if (isalpha (p[i]) && isupper (p[i]))
    {
     printf ("%c", ((p[i] - 'A' + k) % 26) + 'A');
    }

     //check for alphabetic letter and lower cap
     else if (isalpha (p[i]) && islower (p[i]))

     {
         printf("%c", ((p[i] - 'a' + k) % 26) + 'a');
     }
   printf("\n"); 
}  



} // End of codes
0

This is because CS50 expects you to encrypt the alphabetic characters and if the character is not alphabetic leave it as it is but still print out. In your case you have encrypted only the alphabetic characters and printed it out but have ignored the other characters. You might want a add another else if statement saying if a character is not alphabetic, just print out the character as it is.

2
  • Thank you @Vikrant Pradhan.
    – RedRabbit
    Apr 28 '17 at 6:12
  • .... U r wlcom :) Apr 28 '17 at 7:18

You must log in to answer this question.

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