Going through the CS50 exercise Caesar, and I'm still very new to C. My code to validate the key is all working, but as i try to convert from plaintext to the cipher text I keep getting caught up with the conversion form the ASCII number to the char. For example if I run the plaintext with the argv[1] as 27 I get /023/024. I recognize the math is off and if anyone has any pointers about that that would be great, but the main thing I'm curious about is why when I try to assign f into input[i] I get this "/000" type format.
string input = get_string("plaintext: ");
int len = strlen(input);
int i = 0;
while (input[i] != '\0')
{
int mod = atoi(argv[1]);
int t = input[i] + mod;
int f = t % 26;
input[i] = f;
i++;
}
printf("ciphertext: %s\n", input);
Your implementation of doing the %26 is wrong because when you do t % 26 you will get a letter but you have forgotten to add the ASCII value if it is a small of a larger one.
you must change your code to
Here is my code which I had used,
It is correct and hope it helps you...
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2) //checking if there are 2 arguments while calling the main method the first one here will be "./caesar" and the second will be the input given
{
printf("Wrong command\n");
return 1;
}
int k = atoi(argv[1]);//converting string input to integer
string a = argv[1];
for (int i = 0; i < strlen(a); i++)//checking if the input is a number for each character
{
if (a[i] < '0' || a[i] > '9')
{
printf("False\n");
return 1;
}
}
if (k < 0)
{
printf("Wrong command\n");
return 1;
}
else
{
//if everything is successful we ask for the plaintext
string code = get_string("plaintext: ");
//we just print 'ciphertext' and not the 'ciphered text'
printf("ciphertext: ");
for (int i = 0, n = strlen(code); i < n; i++)
{
if (islower(code[i]))
{
printf("%c", (((code[i] + k) - 97) % 26) + 97);
}
else if (isupper(code[i]))
{
printf("%c", (((code[i] + k) - 65) % 26) + 65);
}
else
{
printf("%c", code[i]);
}
}
printf("\n");
return 0;
}
}
Your mistake is that, you are not checking if it is an upper case or lower case and you are not subtracting 97 or 65 before doing the modulus...
If any other doubts, you are free to ask.
Here is the result after running check50 for the 2021 version
:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:) encrypts "barfoo" as "onesbb" using 65 as key
:) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
:) handles lack of argv[1]
All successful
Related
This program is supposed to encrypt strings with the user's key. If the argument count is not 2 and the argv[1] (key) is not a positive number then it prints the usage instructions and returns 1.Else it does the rest of the program.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//Get the key
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
for (int key = 0; key < strlen(argv[1]); key++)
{
if(isalpha(argv[1][key]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
//Convert string to int
int key = atoi(argv[1]);
//Get the plaintext
string plaintext = get_string("Plaintext: ");
//Output the ciphertext
printf("ciphertext: ");
for (int i = 0, length = strlen(plaintext); i < length; i++)
{
if(!isalpha(plaintext[i]))
{
printf("%c", plaintext[i]);
continue;
}
int offset = isupper(plaintext[i]);
for (int j = 0; plaintext[i] != '\0'; i++)
{
if (isalpha(plaintext[i]) != 0)
{
if (isupper(plaintext[i]) != 0)
{
printf("%c", ((plaintext[i] - 65 + key) % 26) + 65);
}
else
{
printf("%c", ((plaintext[i] - 97 + key) % 26) + 97);
}
}
else
{
printf("%c", plaintext[i]);
}
}
int p = plaintext[i] - offset;
int c = (p + key) % 26;
printf("%c", c + offset);
}
printf("\n");
return 0;
}
These are the check50 results:
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\...", not "ciphertext: b\..."
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
output not valid ASCII text
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
output not valid ASCII text
:( encrypts "barfoo" as "onesbb" using 65 as key
expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "ciphertext: ia..."
:) handles lack of argv[1]
:) handles non-numeric key
:) handles too many arguments
But I can't figure out where's the problem. It literally says
-expected "ciphertext: b...", not "ciphertext: b..."
They seem completely same to me.
Edit:
I've changed 65 and 97 to 'a' and 'A'.That solved the ASCII value problem.
I also removed this line.But the problems still remain.
for (int j = 0; plaintext[i] != '\0'; i++)
I've figured out everything and it passed the check50 tests.Thank you for all the help.My solution is below the comments.
I've noticed that things get complicated after the second for loop and I removed everything
int offset = isupper(plaintext[i]);
to
printf("%c", c + offset);
And then I created a new function called mix_text and add that function with a %s in the line of printing ciphertext.This new function takes plaintext and key as its inputs and returns a string.Here's how it looks.
string mix_text(string plaintext, int key)
{
string ciphertext = plaintext;
for (int i = 0, len = strlen(plaintext); i < len; i++)
{
if (isupper(plaintext[i]))
{
ciphertext[i] = (plaintext[i] - 'A' + key) % 26 + 'A';
}
else if (islower(plaintext[i]))
{
ciphertext[i] = (plaintext[i] - 'a' + key) % 26 + 'a';
}
}
return ciphertext;
When I try to submit my work, the system told me to use the exit code. When I use return 0 and recheck, the system told me to use return 1...
(https://i.stack.imgur.com/dnVLV.png)
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\...", not "ciphertext: b"
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "ciphertext: is..."
:( handle lack of argv[1]
expected exit code 1, not 0
:( handles non-numeric key
timed out while waiting for program to exit
:( handles too many arguments
expected exit code 1, not 0
How can I fix it and what's wrong with my code?
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int ok;
char r1;
if (argc == 2)
{
for (int i = 0, s = strlen(argv[1]); i < s; i++)
{
if (!isdigit(argv[1][i]))
{
printf("Sorry\n");
return 0;
}
else
{
ok = atoi(argv[1]);
string c = get_string("Enter:");
printf("ciphertext: ");
for (int t = 0, a = strlen(c); t < a; t++)
{
if (c[t] < 91 && c[t] > 64)
{
r1 = (c[t] - 64 + ok) % 26 + 64;
printf("%c", r1);
}
else if (c[t] < 123 && c[t] > 96)
{
r1 = (c[t] - 96 + ok) % 26 + 96;
printf("%c", r1);
}
else
{
printf("%c", c[t]);
}
}
return 0;
}
}
}
else
{
printf("Sorry\n");
}
printf("\n");
return 0;
}
I try to do well with my homework and all green...
There are multiple issues in your code:
you should return a non zero exit status upon error.
if the number given as a command line argument has more than 1 digit, you perform multiple iterations (one for each digit). You should move the encoding loop out of the first for loop.
using hard coded ASCII values for upper and lower case letters makes the code less portable and hard to read. You should use character constants 'A', 'Z', etc.
r1 = (c[t] - 64 + ok) % 26 + 64; is incorrect and may produce # instead of Z for some inputs. You should use r1 = (c[t] - 65 + ok) % 26 + 65; or better r1 = (c[t] - 'A' + ok) % 26 + 'A';
same mistake for r1 = (c[t] - 96 + ok) % 26 + 96;
Here is a modified version:
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "missing argument\n");
return 1;
}
char *arg = argv[1];
char *p;
int shift = (int)strtol(arg, &p, 10);
if (!(arg[0] >= '0' && arg[0] <= '9') || p == arg || *p != '\0') {
fprintf(stderr, "invalid shift argument: %s\n", arg);
return 1;
}
char *s = get_string("Enter string: ");
printf("ciphertext: ");
for (int t = 0; s[t] != '\0'; t++) {
unsigned char c = s[t];
/* assuming ASCII: upper and lowercase letters are contiguous */
if (c >= 'A' && c <= 'Z') {
c = (c - 'A' + shift) % 26 + 'A';
} else
if (c >= 'a' && c <= 'z') {
c = (c - 'a' + shift) % 26 + 'a';
}
putchar(c);
}
putchar('\n');
return 0;
}
You use the exit code by adding a return <value>, at the appropriate code line.
With <value> being what matches your interface definition, in case of your online judge it seems to be a 1.
In your code you at least fail to do so here:
else
{
printf("Sorry\n");
}
which should be
else
{
printf("Sorry\n");
return 1;
}
An alternative is the more explicit https://en.cppreference.com/w/c/program/exit for situations in which the path to the end of the program is not as obvious.
(This is mostly what the comment by Lundin mentions. I turned it into an explicit answer.)
However, to completely satisfy the judge you need to work on your output.
With the info given in the question, a solution for those problems is not possible.
I'm doing the CS50 Caesar problem and for the most part, my code works. I am not able to pass one of the check50 tests - my code does not handle non-numeric keys, and it timed out while waiting for the program to exit.
I have tried utilizing isdigit but it does not seem to work.
The check50 tests results copied pasted below:
:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:) encrypts "barfoo" as "onesbb" using 65 as key
:) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
:) handles lack of key
:( handles non-numeric key
timed out while waiting for program to exit
:) handles too many arguments
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main (int argc, string argv[])
{
if (argc == 2 && (isdigit(argv[1] !=0))
{
int k = atoi (argv[1]); // convert string to int
string s = get_string ("plaintext: "); // obtain text
printf("ciphertext: ");
for (int i = 0; i < strlen(s); i++) // text loop
{
if (s[i] >= 'a' && s[i] <= 'z')
{
printf("%c", 'a' + ((s[i] - 'a') + k) % 26);
}
else if (s[i] >= 'A' && s[i] <= 'Z')
{
printf("%c", 'A' + ((s[i] - 'A') + k) % 26);
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
return 0;
}
else
{
printf("./caesar key\n");
}
return 1;
}
I guess that the timing out is happening because your program is waiting for plaintext while the judge is not giving that because it excepts your program to exit right after giving non-numeric key.
You can use strtol(), which accepts a pointer to pointer to character and saves position of first invalid character.
Then, you can check if the input is numeric by checking if the returned pointer is pointing at the terminating null charcter.
char* p;
int k = (int)strtol (argv[1], &p, 10);
if (*p != '\0') {
puts("non-numeric key");
return 1;
}
Just loop through every digit of argv[1] and check if it's an integer.
for (int i = 0; i < strlen(argv[1]); i++)
{
if (isdigit(argv[1][i]) == 0)
{
return 1;
}
}
I have created the following code as an answer to CS50x PSET2: Vigenere and it works to some extent however when running through check50 I get some errors listed below:
:) vigenere.c exists.
:) vigenere.c compiles.
:) encrypts "a" as "a" using "a" as keyword
:( encrypts "barfoo" as "caqgon" using "baz" as keyword - output not valid ASCII text
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword - output not valid ASCII text
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
:( encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword- output not valid ASCII text
:( encrypts "hello, world!" as "iekmo, vprke!" using "baz" as keyword- output not valid ASCII text
:) handles lack of argv[1]
:) handles argc > 2
:( rejects "Hax0r2" as keyword - timed out while waiting for program to exit
What seems to be happening is where the key contains a high value (i.e z/Z) it causes the code to skip to the next line and miss out what appears to be random sequences. eg. in the first word of the string it missed out the 3rd character, then the second word it misses the 3rd and 4th and then the third word the 1st. I just can't understand what is happening.
I have used printf to ensure that all the variables being set and passed into functions are correct at runtime. The functions themselves are returning the correct responses (except validation of Hax0r2). I have tried debugging by comparing results to an online vigenere cipher tool.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int Validate1(int argc);
int Validate2(string argv);
void Cypher(string x);
void KeyCalc(string argv);
string MESSAGE;
int LENGTH;
int *KEY;
int COUNTER = 0;
int main(int argc, string argv[])
{
//Check if right amount of arguments are supplied
int Val1 = Validate1(argc);
if (Val1 == 0)
{
//Check if argument is a string of chars
int Val2 = Validate2(argv[1]);
if (Val2 == 0)
{
//get the string length
LENGTH = strlen(argv[1]);
//Dynamically update KEY array length
KEY = (int *)malloc(LENGTH * sizeof(*KEY));
if (KEY == NULL)
{
fprintf(stderr, "malloc failed\n");
}
//calculate the key
KeyCalc(argv[1]);
//get the message from the user to be encrypted
MESSAGE = get_string("plaintext: ");
printf("ciphertext: ");
//encrypt message from user
Cypher(argv[1]);
free(KEY);
return 0;
}
else
{
//validation failed
printf("Usage: ./vigenere keyword\n");
return 1;
}
}
else
{
//validation failed
printf("Usage: ./vigenere keyword\n");
return 1;
}
}
//Validate the number of arguments supplied
int Validate1(int argc)
{
if (argc != 2)
{
return 1;
}
else
{
return 0;
}
}
//Validate the argument is a string
int Validate2(string argv)
{
int k = 0;
//loop through all characters in argument line string and check if alphabetic
for (int i = 0; i < LENGTH; i++)
{
if isalpha(argv[i])
{
//Do Nothing
}
else
{
k++;
}
}
//k counts the number of non-alphabetic characters, so if > 0 then invalid input
if (k > 0)
{
return 1;
}
else
{
return 0;
}
}
void Cypher(string x)
{
//identify the length of the message to be coded
int Mlength = strlen(MESSAGE);
//identify the length of the key
int Slen = strlen(x);
//cycle through all characters in message supplied by user
for (int i = 0; i < Mlength; i++)
{
// loop through key
if (COUNTER > Slen - 1)
{
COUNTER = 0;
}
//check if the character is alphabetic
if (isalpha(MESSAGE[i]))
{
//convert the character to ASCII int value
char l = MESSAGE[i];
//add key value to message value and wrap around ascii mapping
if (isupper(MESSAGE[i]))
{
l = l + KEY[COUNTER];
if (l > 'Z')
{
l = l - 26;
}
}
else
{
l = l + KEY[COUNTER];
if (l > 'z')
{
l = l - 26;
}
}
//convert value back into character and store in array
MESSAGE[i] = (char) l;
// print character
printf("%c", MESSAGE[i]);
COUNTER++;
}
else
{
//character is 'numeric' or 'symbol' or 'space' just display it
printf("%c", MESSAGE[i]);
}
}
printf("\n");
}
void KeyCalc(string argv)
{
//convert key entry to values A/a = 0 to Z/z = 26
for (int i = 0; i < LENGTH; i++)
{
char k = argv[i];
if (islower(argv[i]))
{
KEY[i] = k - 'a';
}
else
{
KEY[i] = k - 'A';
}
}
}
encrypts "barfoo" as "caqgon" using "baz" as keyword
encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword
encrypts "hello, world!" as "iekmo, vprke!" using "baz" as keyword
rejects "Hax0r2" as keyword
From the spec for the caesar pset:
...Caesar’s algorithm (i.e., cipher) encrypts messages by
“rotating” each letter by k positions. More formally, if p is some
plaintext (i.e., an unencrypted message), pi is the ith character in
p, and k is a secret key (i.e., a non-negative integer), then each
letter, ci, in the ciphertext, c, is computed as
ci = (pi + k) % 26
This algorithm (in either "case") does not do that:
l = l + KEY[COUNTER];
if (l > 'Z')
{
l = l - 26;
}
This walkthrough starting at 9:30 is a good primer on how to implement the "shift".
The proximate cause of the problem in this code is that this l = l + KEY[COUNTER]; can yield a result outside the ascii range. In the CS50 implementation, char defaults to a signed char. So, for example, 'r' + 'z' (as in "barfoo" ciphered with "baz") will yield -117.
Basically I have to create a caesar cipher, which is solely replacing each letter given with a letter that is int 'k' away. This takes 2 command line arguments: './caesar' and 'k', which given by the user. it works fine; but has one issue:
it encrypts "BARFOO" as "EDUIRR" using 3 as key which is correct
encrypts "BaRFoo" as "FeVJss" using 4 as key which is correct
BUT it does not encrypt "barfoo" as "onesbb" using 65 as key, it encrypts it as "oonneess|bb|bb" .
Please notice the punctuation; the caps and so on.
See the problem here? it also does this for other random words; it repeats letters. Help me....
PS: I am extremely new to programming, as you can see in my code, so please try to explain in english!
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
string s;
//int d;
int c;
int a;
if(argc != 2)
{
printf("Please run with a command line argument.");
return 1;
}
else
{
s = GetString();
}
int k = atoi(argv[1]);
for(int i = 0; i < strlen(s); i++)
{
a = s[i];
if(a<'A'||a>'z')
{
printf(" ");
}
else
{
if(a>='A'&&a<='Z')
{
c = a+k;
while(c>'Z')
{
c = 'A'+(c-'Z')-1;
printf("%c", c);
}
if(c<='Z')
{
printf("%c", c);
}
}
else if(a>-'a'&&a<='z')
{
c = a+k;
while(c>'z')
{
c = 'a'+(c-'z')-1;
printf("%c", c);
}
if(c<='z')
{
printf("%c", c);
}
}
}
}
printf("\n");
}
You can try to take k%26 since it should wrap around the characters of alphabet.
That should solve your problem.
You should try this way. Take the case of uppercase letters. First get the index from the letter 'A'.
index = a - 'A';
Then add the value in the variable k and get the remainder when divided with 26.
modified_index = ( index + k ) % 26;
Now to get the desired letter just add it with 'A'.
c = 'A' + modified_index;
Only adding k%26 will not help as that way 'z' with increment 1 will be turned into { which is wrong.
Also, if you just add value in k with the letter denoted by variable a it might cross the limit of ASCII characters as Joulin mentioned.