Reputation: 1
I simply cannot detect why I can't make isalpha working for the assignment of Vigenere. I've tried so many different inputs. I'm a complete beginner with programming - as in - absolute beginner, so I am wondering whether you have any thoughts. I've also tried to put in argv and argc into isalpha, but it still did not have the output I wanted. The user has to see an error if the keyword that is used is not a letter. This is my code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc < 2)
{
printf("ERROR \n");
return 1;
}
if (! isalpha (k[i]))
else
{
string s = get_string("plaintext: ");
int k = atoi(argv[1]);
printf("ciphertext: ");
for (int i = 0, n = strlen(s); i < n; i++)
if (isalpha(s[i]))
{ printf("ERROR");
if isupper(s[i])
{
printf("%c", (((s[i] + k) - 65) % 26));
}
else if islower(s[i])
{
printf("%c", (((s[i] + k) - 97) % 26));
}
else
{
printf("%c", (s[i]));
}
}
}
{
printf("\n");
}
}
Upvotes: 0
Views: 76
Reputation: 5051
Your source code has a number of errors.
if (! isalpha (k[i]))
else
Two problems with this code fragment:
You attempt to convert the first command line argument to an integer when its first character may be an alpha character:
int k = atoi(argv[1]);
I am not sure what you think k contains at this point, but whatever you think it is, you are wrong. You probably want to convert all but the first character into an int.
What kind of data is argv[1][0], assuming a command line argument was properly used when starting the program? Are you looking for two particular characters, or will any alpha characters suffice?
Upvotes: 1