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

int main(int argc , string argv[])
{ 
    if (argc == 2)
    {
        if ( atoi(argv[1]) < 1 )
        {
            printf("Usage:./caesar key\n");
        }
        else
        { 
        printf("Sucess\n");
        }
    }
    
    string plaintext = get_string("plaintext : ");
    printf("ciphertext: ");

    
    int remainder = ( atoi(argv[1]) % 26);
    for ( int i = 0 , n = strlen(plaintext) ; i < n ; i++)
    {
        if (isupper(plaintext[i]))
        {
            if ( plaintext[i] + remainder > 90 )
            {
                printf("%c" , plaintext[i] - (26- remainder));
            }
            else
            {
                printf("%c",plaintext[i] + remainder ); 
            }
        }
        else if (islower(plaintext[i]))
        {
           if ( plaintext[i] + remainder > 122)
            {
                printf("%c", plaintext[i] - (26- remainder));
            }
            else
            {
                printf("%c",plaintext[i] + remainder ); 
            }  
        }
        else 
        {
            printf("%c",plaintext[i]);
        }
    }
    printf( " \n ");
    return 0;
}

enter image description here

:( encrypts "barfoo" as "yxocll" using 23 as key expected "ciphertext: yx...", not "ciphertext: yx..."

1

The main problem is this:

printf( " \n ");

The spec says to print a line feed after the encrypted code. This prints an extra space before and after the line feed. One of the hidden lessons in the class is that you must code EXACTLY to the specification, close doesn't work. This is a critical lesson, especially in team programming where different people work on different parts.

As for the other errors, you need to work on validating the parameters. Since most of that code doesn't exist, I'll let you work on that. IF you have problems with it, please open a new question.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • thankyou so much.... can you explain to me what does validating the parameters mean, cause there is still an error that says " timed out while waiting for program to exit" . Sep 28 '20 at 8:17
  • validating the parameters means checking to see that when the program is run, there is one and only one parameter, the "key" , and that it is indeed a number. If the tests are waiting for the program to exit, it means there's a missing return statement.
    – Cliff B
    Sep 28 '20 at 8:30
  • Thank you so much... it worked.. Sep 28 '20 at 13:55

You must log in to answer this question.

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