CS50 caesar.c - ciphertext printing out of ASCII code range - c

I'm going through CS50 (2021x version) of Caesar problem, and ran into problem. My program is printing outside of ASCII range (thanks to curiouskiwi over at discord for this hint). The error message says ":( encrypts "barfoo" as "yxocll" using 23 as key, output not valid ASCII text". Another one I'm having trouble is "world, say hello!", for same reason (Not valid ASCII text). Other ones are encrypting fine.
I've stepped through the debugger and found that 'letter' variable is sometimes becoming a negative integer like -119'/211', but can't figure out why that may be so. I expected to see positive value associated with an alphabet in ASCII. When this happens the letters will stop printing on the console.
If I type ./caesar 23 | cat -A and then give barfoo as plaintext, the cyphertext will come out as yxM-^IcM-^FM-^F$.
int main(int argc, string argv[])
{
// only 1 arugment, and positive argument only
if (argc == 2 && argv[1] > 0)
{
// check if each char of argument is digit
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isdigit(argv[1][i]))
{
// do nothing
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
// change the key to how much letters should move over
int input = atoi(argv[1]);
int key = input % 26;
char letter;
// get the input
string text = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isalpha(text[i])) // if it is an alphabet
{
if (islower(text[i])) // if it is lowercase
{
letter = text[i] + key; // add key to text[i]
if (letter > 122)
{
// loop around the alphabet
letter -= 26;
}
printf("%c", letter);
}
else // if it is uppercase
{
letter = text[i] + key; // add key to text[i]
if (letter > 90)
{
// loop around the alphabet
letter -= 26;
}
printf("%c", letter);
}
}
else // if it is not an alphabet
{
printf("%c", text[i]);
}
}
printf("\n");
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}

You should use int letter instead of char letter.
This is because
letter = text[i] + key;
will overflow signed char for, say, 'z' + 23.

Related

CS50 Caesar program is working but check50 says it isn't

I created this program but I'm getting errors on CS50 showing that I didn't do any of it correctly.
The requirements are as follows:
Implement your program in a file called caesar.c in a directory called caesar.
Your program must accept a single command-line argument, a non-negative integer. Let’s call it k for the sake of discussion.
If your program is executed without any command-line arguments or with more than one command-line argument, your program should print an error message of your choice (with printf) and return from main a value of 1 (which tends to signify an error) immediately.
If any of the characters of the command-line argument is not a decimal digit, your program should print the message Usage: ./caesar key and return from main a value of 1.
Do not assume that k will be less than or equal to 26. Your program should work for all non-negative integral values of k less than 2^31 - 26. In other words, you don’t need to worry if your program eventually breaks if the user chooses a value for k that’s too big or almost too big to fit in an int. (Recall that an int can overflow.) But, even if k is greater than 26, alphabetical characters in your program’s input should remain alphabetical characters in your program’s output. For instance, if k is 27,
A should not become [ even though [ is 27 positions away from A in ASCII, per http://www.asciichart.com/[asciichart.com]; A should become B, since B is 27 positions away from A, provided you wrap around from Z to A.
Your program must output plaintext: (without a newline) and then prompt the user for a string of plaintext (using get_string).
Your program must output ciphertext: (without a newline) followed by the plaintext’s corresponding ciphertext, with each alphabetical character in the plaintext “rotated” by k positions; non-alphabetical characters should be outputted unchanged.
Your program must preserve case: capitalized letters, though rotated, must remain capitalized letters; lowercase letters, though rotated, must remain lowercase letters.
After outputting ciphertext, you should print a newline. Your program should then exit by returning 0 from main.
My code:
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
//check if k inputed
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
//value k is the number after ./caesar
int k = atoi(argv[1]) % 26;
int x = 0;
int s = strlen(argv[1]);
//check if k is a positive integer
if (k < 0)
{
printf("Usage: .caesar key\n");
return 1;
}
else
{
//check for arguments
for (int i = 0; i < s; i++)
{
if (isalpha (argv[1][i]))
{
continue;
}
else if (isalnum (argv[1][i]))
{
x++;
}
else
{
continue;
}
}
if (x != s)
{
printf("Usage: ./caesar key\n");
}
else if (x == s)
{
//get plaintext
string plain_text = get_string("plaintext: ");
printf("ciphertext: ");
for (int y = 0; y <= strlen(plain_text); y++)
{
//change letters
if (isalpha(plain_text[y]))
{
char p = plain_text[y];
int cipher_int = p + k;
if (isupper(p))
{
while(cipher_int >= 90)
{
cipher_int -= 26;
}
char cipher_text = cipher_int;
printf("%c", cipher_text);
}
if (islower(p))
{
while(cipher_int >= 122)
{
cipher_int -= 26;
}
char cipher_text = cipher_int;
printf("%c", cipher_text);
}
}
else
{
printf("%c", plain_text[y]);
}
}
printf("\n");
}
}
return 0;
}
It appears that your wrapping is not working correctly. I found that when I used 3 as the key and put "The quick fox jumps over the lazy brown dog." as the plain text, "brown" became "eur`q" when it should be "eurzq". I think you're using >= in your wrapping comparisons when you should use >.
Your check for digits is very cumbersome and does not cause the program to return 1 as required if the argument is incorrect.
Here is a simpler test:
//check for arguments
for (int i = 0; i < s; i++) {
if (!isdigit((unsigned char)argv[1][i])) {
printf("Usage: ./caesar key\n");
return 1;
}
}
Also note that you should stop the encoding loop when the index == the length of the string. therefore the operator should be <.
Another problem is the use of isalpha() and similar functions from <ctype.h> with char values. These functions are undefined for negative values (except EOF). Some platforms define char as signed by default, making isalpha(plaintext[y]) have undefined behavior if the user typed non ASCII text. Cast the argument as (unsigned char) to avoid this problem.
Furthermore, you should not use hardcoded ASCII values such as 90 and 122, use character constants such as 'a' and 'z' for better readability. Doing so would make another error in your encoding loop more obvious: while(cipher_int >= 90) should be if (cipher_int > 'A') and while(cipher_int >= 122) should be if(cipher_int > 'z').
Here is a modified version:
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
// check for a single command line argument
if (argc != 2) {
printf("Usage: ./caesar key\n");
return 1;
}
char *arg = argv[1];
if (*arg == '\0') {
printf("caesar: key cannot be an empty string\n");
return 1;
}
// check that the argument is a non negative number
for (size_t i = 0; arg[i]; i++) {
if (!isdigit((unsigned char)arg[i])) {
printf("Usage: ./caesar key\n");
return 1;
}
}
// value k is the shift number after ./caesar
int k = atoi(argv[1]) % 26;
// get plaintext
string plain_text = get_string("plaintext: ");
printf("ciphertext: ");
for (size_t i = 0; plain_text[i] != '\0'; i++) {
unsigned char c = plain_text[i];
// change letters
if (islower(c)) {
putchar('a' + ((c - 'a') + k) % 26);
} else
if (isupper(c)) {
putchar('A' + ((c - 'A') + k) % 26);
} else {
putchar(c);
}
}
printf("\n");
return 0;
}

All outputs end up the same in C

I'm trying to iterate over key which has been entered into the command line. During the iteration, I want to create 2 strings, upper and lower, which can be used later. The problem is that key, upper and lower are all ending up with the same outputs. Could someone help me figure out where I'm going wrong?
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./substitution key\n"); // if no key is entered on command line, end program and say key
return 0;
}
string key = argv[1]; // convert command to key for ease
string alpha = "abcdefghijklmnopqrstuvwxyz"; // lowercase alphabet
string cap_alpha ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //uppercase alphabet
string lower = argv[1]; // gives string correct length to edit later
string upper = argv[1]; // ^ditto
if (strlen(key) != 26) // if key is not exactly 26 char, end program and say it must contain 26
{
printf("Key must contain 26 characters\n");
return 0;
}
for (int i = 0; i < strlen(key); i++) //iterates through key
{
if (key[i] < 'A' || (key[i] > 'Z' && key[i] < 'a') || key[i] > 'z') //if key includes non alphabet characters, ends command
{
printf("Key must only contain alphabetic characters.\n");
return 0;
}
else if (key[i] >= 'A' && key[i] <= 'Z')
{
lower[i] += 32; // if char in key is uppercase, convert to lowercase, add to string "lower"
upper[i] = key[i]; // if char in key is uppercase, keeps as uppercase
}
else
{
upper[i] = key[i] - 32; // if char in key is lowercase, changes to uppercase
lower[i] = key[i];
}
}
printf("key: %s\n", key); // test output
printf("lower: %s\n", lower); //test output
printf("upper: %s\n", upper); // test output
}
Assigning strings doesn't make copies, it just assigns a pointer to the same string. So key, upper, lower, and argv[1] are all the same string. When you make a change to one of them, it affects all of them.
You need to make copies first.
size_t len = strlen(key) + 1; // +1 for the null byte
char upper[len], lower[len];
strcpy(upper, key);
strcpy(lower, key);
BTW, C has functions isalpha(), isupper(), islower(), toupper(), and tolower(). You should use these instead of relying on the specifics of ASCII coding.

Shift problem of cipher with Vigenere in C

I am extremely new to programming and I'm having some difficulties with Vigenere in C from the edX course CS50. I have broken the problem down into uppercase letters and lowercase letters and I am only trying to solve the uppercase letter problem right now. I am using the word 'panda' as my key and 'ILIKEYOU' as the plaintext. When I run the program, the first letter corresponds to the letter I'd expect it to be (23=X). After that, the program just seems to spit out random numbers for the remaining 7 letters. I haven't converted back to ASCII since I'm having so many problems with my code. Any ideas what is going on? Thank you all so much for the help :)
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
// Print error message if the user imput is executed without any
command-line arguments or with more than one command-line argument
if (argc != 2)
{
printf("Usage: ./vigenere k\n");
return 1;
}
// Access key
string key = argv[1];
// Convert the array from a string to an int
int letter;
letter = atoi(argv[1]);
// Print error message if the user imput is one command-line argument
and contains *any* non-alphabetical character(s)
for (int c = 0; c < strlen(key); c++)
{
if (!isalpha (key[c]))
{
printf("Usage: ./vigenere k\n");
return 1;
}
}
// Prompt the user for a string of plaintext
string p;
p = get_string("plaintext:");
//Print ciphertext
printf("ciphertext: ");
// Accessing each character in the plaintext
for (int i = 0, n = strlen(p); i < n; i++)
{
// Shift letters only in the plaintext
if (isalpha(p[i]))
{
// Convert plaintext and key to ASCII capital letters to
alphabetical index
// Encipher capital letters in plaintext and print
int c = 0;
if (isupper(p[i]))
{
printf("%i\n", ((p[i] - 65) + (toupper(key[c]) - 65)) % 26);
}
}
}
Needs few modifications -
int index_key = 0;
int shift= 0;
int key_len = strlen(key);
for (int i = 0, n = strlen(p); i < n; i++)
{
// Shift letters only in the plaintext
if (isalpha(p[i]))
{
// Convert plaintext and key to ASCII capital letters to
//alphabetical index
// Encipher capital letters in plaintext and print
if (isupper(p[i]))
{
shift = ((p[i] - 'A') + (toupper(key[index_key % key_len]) - 'A')) % 26;
index_key++;
printf("%c", p[i] + shift);
}
}
}

C: Vigenere cipher compiles but segmentation error - pointers?

I'm getting a segmentation error somewhere in this code, though it compiles successfully. The code is to iterate repeatedly through a key to encipher a plaintext word. I'm assuming it's pointer-related but I don't know how to fix it.
Also: is that an appropriate way to express a negative for isalpha? It didn't throw an error. It did throw an error when I tried to use ==FALSE after it.
int main(int argc, char* argv[])
{
if ((argc<2) || (argc>2) || (!isalpha(argv)))//exits if non-alpha key or < or > than 2 command line inputs
{
printf("Please enter your single word key"); //prompts for appropriate key
return 1;
}
if(argc==2) // requires two command line inputs with non-integer key to continue
{
printf("plaintext:"); //Printed on terminal before word to be enciphered
char* word = GetString(); //Prompts user to enter word required to be enciphered
printf("ciphertext:");
char* key = (argv[1]); //key = initial user command line entry
int i = 0;
int k = 0; //defines characters in key
for(i=0; i<strlen(word); i++) //iterates through word entered by user as plaintext
{
if(isalpha(key[k]))
{
if(isupper(word[i])) //if original characters are uppercase
{
int cipher = (word[i] + key[k] -65) % 26 + 65;
printf("%c", cipher);
}
else if(islower(word[i])) //if original characters are lowercase
{
int cipher = (word[i] + key[k] - 97) % 26 + 97;
printf("%c", cipher);
}
else //for all other types of characters
{
printf("%c", word[i]);
}
}
else if(!isalpha(key[k]))
{
return 1;
}
if (word[i]==strlen(word)) //if reaches end of plaintext word
{
printf("\n"); //print a new line and exit
return 1;
}
else if(word[i]<strlen(word)) //if haven't reached end of word, increment k
{
k++;
}
if (key[k]==strlen(key))
{
k = 0;
}
}
printf("\n");
}
}

What isn't working properly with my Vigenere cipher code?

I'm trying to make a Vigenere cipher code in C and I have done something that is wrong and I can't fix it... How do understand that something goes wrong? Well I have some examples with keyword and result cipher with Vigenere cipher like
keyword: bacon
text: Meet me at the park at eleven am
correct result: Negh zf av huf pcfx bt gzrwep oz
my code result with same text and keyword: Tegh ne og tjs qaty bt syfvgb bm
Code:
int main(int argc, string argv[])
{
string keyWord;
if( argc != 2 )
{
printf("Wrong Argument");
return 1;
}
else
{
keyWord = argv[1];
//check if argument is
//only alphabetical characters
for(int i = 0; i < strlen(keyWord); i++)
{
char c = keyWord[i];
if( !isalpha(c) )
{
printf("Your Keyword Must Contain Only alphabetical characters\n");
return 1;
}
}
}
//todo
printf("Enter Plain Text\n");
string plainText = GetString();
for(int i = 0; i < strlen(plainText); i++)
{
char c = plainText[i];
int keyWordWrapper;
char keyC;
if(isalpha(c))
{
keyWordWrapper = i % strlen(keyWord);
keyC = keyWord[keyWordWrapper];
if(islower(c))
{
int key = keyC - 'a';
c = (c - 'a' + key) % 26 + 'a';
}
if(isupper(c))
{
int key = keyC - 'A';
c = (c - 'A' + key) % 26 + 'A';
}
}
printf("%c",c);
}
printf("\n");
return 0;
}
GetString() is declared in a header and defined in a library that I'm using (it's like scanf).
this is the updated code
int main(int argc, string argv[])
{
string keyWord;
if( argc != 2 )
{
printf("Wrong Argument");
return 1;
}
else
{
keyWord = argv[1];
//check if argument is
//only alphabetical characters
for(int i = 0; i < strlen(keyWord); i++)
{
char c = keyWord[i];
if( !isalpha(c) )
{
printf("Your Keyword Must Contain Only alphabetical characters\n");
return 1;
}
}
}
string plainText = GetString();
int j;
for(int i = 0; i < strlen(plainText); i++)
{
j++;
char c = plainText[i];
int keyWordWrapper;
char keyC;
if(j > strlen(keyWord))
j = 0;
if(isalpha(c))
{
keyWordWrapper = i % strlen(keyWord);
keyC = keyWord[keyWordWrapper];
int key;
tolower(c);
if(islower(keyC))
key = keyC - 'a';
if(isupper(keyC))
key = keyC - 'A';
c = (c - 'a' + key) % 26 + 'a';
}
printf("%c",c);
}
printf("\n");
return 0;
}
There are two problems in the code.
First is the treatment of upper case letters in the keyword. Note that in one case, the code subtracts a from keyC, and in the other A is subtracted. But that's based on the case of the plain text character. That subtraction needs to be based on the case of the letter in the keyword.
Second, the code advances to the next character in the keyword for every character in the plain text. The "correct result" doesn't advance to the next character of the keyword if the plain text character is a space character.
Here's an example of what I'm talking about for the second problem
text Meet me at
keyC baco nb ac
i 0123456789 i must always increment to the next char in plain text
k 0123 40 12 index into the keyword does not increment on non-alpha
Therefore k cannot be computed directly from i with the line
keyWordWrapper = i % strlen(keyWord);
Instead k needs to be initialized to 0 and then incremented only when the plain text contains an alpha character. The following line will compute the correct index into the keyword.
keyWordWrapper = k % strlen(keyWord);
The only difference is that i is replaced by k and k only increments when the plain text has an alpha character.
You should convert the key to all lower case (or all upper case) and then use the same expression in both shift blocks:
int key = keyC - 'a'; // Or 'A' if you convert to upper
You should remove the strlen(plainText) from the condition of the for loop; it converts a linear algorithm into a quadratic one.

Resources