I'm going along with Section 3 and printing "BARFOO" in the caesar program. This is the output from terminal:
This has happened many times with multiple programs and I originally assumed it was a problem with my own code. This time, though, the code was from Section 3. Any reason as to why this is happening?
Here is the code (written by CS50 staff):
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
// ensure user entered a valid key
if (argc != 2 || atoi(argv[0]) < 0)
{
printf("Usage: ./caesar key\n");
return 1;
}
// save the key
int key = atoi(argv[1]);
// get the plaintext from the user
string plaintext = GetString();
// encrypt the plaintext
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
{
plaintext[i] = (plaintext[i] - 'A' + key) % 26;
}
else if (plaintext[i] >= 'a' && plaintext[i] < 'z')
{
plaintext[i] = (plaintext[i] - 'a' + key) % 26;
}
}
// print out the encrypted message
printf("%s\n", plaintext);
return 0;
}