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

int main(int argc, string argv[])
{
if ( argc > 2 || argc < 2 )
    return 1;
else
{
    string s = argv[2];
    int key = atoi(s);

    string text = GetString();
    for (int i= 0, j = strlen(text); i<j; i++)
    {
     if ( isalpha(text[i]) )            //Is it alphabet
     {
         if (isupper(text[i]))          //Is it uppercase
         {
             int letter = text[i];
             int ascii = (letter + key);
             if (ascii <= 90)
                printf("%c", ascii);
            else
            {
               int ascii_a =  ascii % 26;
               printf("%c", ascii_a);
            }
         }
         else                           //Is lowercase
         {
             int letter = text[i];
             int ascii = (letter + key);
             if (ascii <= 122)
                printf("%c", ascii);
            else
            {
                int ascii_a = ascii % 26;
                printf("%c", ascii_a);
            }
         }
     }
     else                                   //not alphabet
     {
        printf("%d", text[i]);
     }
    }
    printf("\n");
    return 0;
}
}
2
  • 1
    What are the errors?
    – Cliff B
    Aug 7 '16 at 13:54
  • MARS, please undelete your answer. It is correct. Additionally, the format code for printing non-alpha characters is incorrect.
    – Cliff B
    Aug 11 '16 at 21:43

You must log in to answer this question.

Browse other questions tagged .