I'm a complete newbie trying desperately hard with this pset for the past month. At best I've got the following but this isn't working.
I think my shift function is ok, and I know at the moment it's written to print integers at the end, but even the integers are not the correct ASCII.
If you run the code it spits out four integers even when the length of plaintext was only 3 characters. Please let me know what you think?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int shift (char c); // declaring prototype of a function that converts char into shift value
int main(int argc, string argv[]) // Counting command-line arguments
{
if (argc == 2)
{
string s = argv[1]; // string of user's key
int l = 0;
while (s[l] != '0')
{
l++
}
int i = 0 // validating user's key
while (i < l)
{
if (isalpha(s[i]))
{
i++;
}
else
{
printf("Usage: ./vigenere keyword\n");
return 1;
}
}
string plaintext = get_string("plaintext: "); // prompt for plaintext
{
printf("ciphertext: ");
for (int a = 0, n = strlen(plaintext); a < n; a++)
{
int key = shift(s[l]);
int ciphertext = (int)plaintext[a];
{
printf("%i + %i", ciphertext, key);
}
l++;
}
printf("\n");
}
}
else
{
printf("Usage: ./vigenere keyword\n");
return 1;
}
}
int shift(char c) // Fx shift: getting the integer of key for each char
{
int key = (int)c;
if (65 <= key && key <= 90)
{
return (key - 65);
}
else if (97 <= key && key <= 122)
{
return (key - 97);
}
else
{
return 1;
}
}