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

bool string_validity(string s);

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



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

        if(!isdigit(s[i]))
        
            return false;
        
     return true;
    
}

it looks like it would work but I keep getting errors such as "i is undeclared" etc.

clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    ceaser.c  -lcrypt -lcs50 -lm -o ceaser
ceaser.c:20:47: error: relational comparison result unused [-Werror,-Wunused-comparison]
    for(int i = 0, len = strlen(plaintext); i < len, i++)
                                            ~~^~~~~
ceaser.c:20:57: error: expected ';' in 'for' statement specifier
    for(int i = 0, len = strlen(plaintext); i < len, i++)
                                                        ^
ceaser.c:31:27: error: use of undeclared identifier 'p'
        printf("%c", (c - p + key) % 26 + p);
                          ^
ceaser.c:31:43: error: use of undeclared identifier 'p'
        printf("%c", (c - p + key) % 26 + p);
                                          ^
ceaser.c:32:9: error: expected expression
        else
        ^

1 Answer 1

1
for(int i = 0, len = strlen(plaintext); i < len, i++)

The standard structure of a for loop is:

for (initialization statement; test expression; update statement)

You have a comma instead of a semicolon between your test expression and your update statement, meaning the compiler thinks your test expression or conditional is "i < len, i++" (which means something quite different in C) and the update statement is missing.

if (isalpha(c))
{
    char p = 'A';
}
else if (islower(c))
{
    char p = 'b';
}
printf("%c", (c - p + key) % 26 + p);
else
{
    printf("%c",c);
}

Variables defined inside an if block are scoped to that block. In other words, char p is not accessible to code outside of that specific if { ... } block. Your printf statement after the second else if statement signifies to the compiler that you are done with this sequence of if...elses, and so the next else is seen as invalid because it is not associated with an initial if statment.

You must log in to answer this question.

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