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

int main( int argc,  string argv[])
{

    if(argc!=2)
    {
        printf("One more string \n");
    }
    string key = argv[1];
    else if(!isalpha(key))
    {
        printf("Prompt only alphabet letters\n");
    }
    else 
    {
        string p = GetString();

        for ( i=0, j=0, n=strlen(p); i<p; i++, j++)
        { 
            if(j>=stlrlen(p))
            {
                j=0;
            }

            if(isupper(p[i]))
            {
                printf("%c", ((p[i] +key[j])%26)+'A');
            }

            if(islower(p[i]))
            {
                printf("%c", ((p[i] + key[j])%26)+'a');
            }

            if(!isupper(p[i]) && !islower(p[i]))
            {
                printf("%c", p[i]);
            }
            printf("\n");
            return 0;
        }
    }
}

What's wrong with my code? CS50 Appliance brings me 1 error

c:15:1 expected expression.

Does it make any sense in my code? Can someone help me? When I'm just deleting else if block of code, it brings me the same error to else statement further.

2
  • 1
    The first else has no corresponding if. Did you mean to include string key = argv[1]; in one of the blocks?
    – nnn
    Nov 29, 2015 at 22:58
  • 3
    You can't have code outside of the scope of the if or else in between the if-else Nov 29, 2015 at 22:58

2 Answers 2

2

I believe the problem is that you cannot have a statement split up an if and else if or if and else

if(argc!=2)
{
    printf("One more string \n");
}
string key = argv[1]; // This is the problem ➞ it cannot sit in the middle of if and else or else if
else if(!isalpha(key))
{
    printf("Prompt only alphabet letters\n");
}

Furthermore, you probably mean to make the else if and the else regular if statements because it looks like you are doing some validation

Also, since you in your for-loop you have i<p but p is a string. Furthermore, since you don't update n in the loop you should initialize it outside

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

Also despite declaring n and assigning it strlen( p ), you use the function call inside your for-loop. There's nothing wrong with that, it just might make more sense to use the variable you assigned.

1

You can't have code between an if and an else if statement.

string key = argv[1]; cannot be there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.