#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.
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.
You can't have code between an if and an else if statement.
string key = argv[1];
cannot be there
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.