0

The code is following:

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

bool key_validity(string s);
int main(int argc, string argv[])
{
    if(argc != 2 || ! key_validity(argv[1]))
    {
        printf("Usage: ./caesar key");
        return 1;
    }
    int key = atoi(argv[1]);
    string plaintext = get_string("plaintext : ");
    printf("ciphertext: ");
    for (int i = 0, len = strlen(plaintext); i < len; i++)
    {
        char c = plaintext[i];
        if (isalpha(c))
        {
            char p = 'A';
            if (islower(c))
            {
                p = 'a';
            }
            printf("%c", (c - p + key) % 26 + p);

        }
        else
        {
            printf("%c",c);
        }
    }
   
    printf("\n");
}

bool key_validity (string s)
{
    for (int i = 0, len = strlen(s); i < len; i++)

        if(!isdigit(s[i]))

            return false;

    return true;


}

variable "len" is never defined as an integer, yet the program works and terminal doesn't ask to define it. How come?

0

You can define multiple variables of the same type by separating them with a comma:

int i, j, k;

These are all taken to be integers by the compiler. You can further add values to initialise the variables:

int i = 0, j = 1;

In your code's syntax, both i and len are defined as integers.

You must log in to answer this question.

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