Skip to content

Instantly share code, notes, and snippets.

@teeschorle
Last active April 7, 2020 20:42
Show Gist options
  • Save teeschorle/3e5c87cdc0b6b7fefcfbabd891004d5a to your computer and use it in GitHub Desktop.
Save teeschorle/3e5c87cdc0b6b7fefcfbabd891004d5a to your computer and use it in GitHub Desktop.
CS50 Problem Set 2 (Fall 2019) - Caesar
//CS50 Problem Set 2 (Fall 2019): Substitution
//Author: teeschorle
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int upperbound(char c);
int main(int argc, string argv[])
{
if(argc == 2)
{
int digits = strlen(argv[1]);
bool isnum = true;
for(int i = 0; i < digits; i++)
{
if(isdigit(argv[1][i]) == 0)
{
isnum = false;
// printf("isnum %i false", i);
break;
}
}
if(isnum == true)
{
int rotateby = atoi(argv[1]) % 26;
string plaintext = get_string("plaintext: ");
int charcount = strlen(plaintext);
char ciphertext[charcount + 1]; //+ 1 wegen null-character; wichtig: strlen(ciphertext) wäre unbrauchbar, da der String noch leer ist (Position der Null ist willkürlich)
ciphertext[charcount] = '\0'; //nur so klappt das dann auch mit strlen (weil das Programm sonst das Ende des Strings nicht findet)
for (int i = 0; i < charcount; i++)
{
if (isalpha(plaintext[i]) != 0 && (int) plaintext[i] + rotateby <= upperbound(plaintext[i]))
{
ciphertext[i] = (char) ((int) plaintext[i] + rotateby); //achtung, Overvflow!! (wieder vorne anfangen; Trennung upper/lower)
}
else if (isalpha(plaintext[i]) != 0 && (int) plaintext[i] + rotateby > upperbound(plaintext[i]))
{
ciphertext[i] = (char) ((int) plaintext[i] + rotateby - 26);
}
else
{
ciphertext[i] = plaintext[i];
}
}
printf("ciphertext: %s\n", ciphertext);
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
else
{
printf("Please provide us with ONE argument.\n");
return 1;
}
}
int upperbound(char c)
{
if (isupper(c) == 0) //=> is lower
{
return 122;
}
else
{
return 90;
}
}
@TetyanaHvozda
Copy link

Hi! Could you please explain your codes 16-24 (see copied below)? What do you mean by "isnum" and specifically what do you mean by line 20 ( if(isdigit(argv[1][i]) == 0))
bool isnum = true;
for(int i = 0; i < digits; i++)
{
if(isdigit(argv[1][i]) == 0)
{
isnum = false;
printf("isnum %i false", i);
break;

Thank you in advance! =)

@teeschorle
Copy link
Author

teeschorle commented Apr 7, 2020

Hey @TetyanaHvozda, sorry for the late reply. The code lines 16 to 24 (or 18 to 26 more specifically) test whether the key provided in the command line (second element of argv, therefore argv[1]) is purely numeric. The outcome of this test is reflected by the boolean variable isnum, which is set to „true“ as initial value. The for-loop loops through the key, character by character, and tests if each is indeed a digit, by using the function isdigit(). This function returns zero, if the character is not a digit - in that case the isnum variable is set to „false“, which ultimately results in an error message (lines 52 to 56).
The printf function in line 23 is not essential for the programme to run successfully, indeed I should have commented it out, as I mainly used it for debugging to see which character of the key is not numeric. I‘ll amend the code accordingly (but the CS50 assessment routine still accepted the code).
Hope that helped :)

@TetyanaHvozda
Copy link

Hey @TetyanaHvozda, sorry for the late reply. The code lines 16 to 24 (or 18 to 26 more specifically) test whether the key provided in the command line (second element of argv, therefore argv[1]) is purely numeric. The outcome of this test is reflected by the boolean variable isnum, which is set to „true“ as initial value. The for-loop loops through the key, character by character, and tests if each is indeed a digit, by using the function isdigit(). This function returns zero, if the character is not a digit - in that case the isnum variable is set to „false“, which ultimately results in an error message (lines 52 to 56).
The printf function in line 23 is not essential for the programme to run successfully, indeed I should have commented it out, as I mainly used it for debugging to see which character of the key is not numeric. I‘ll amend the code accordingly (but the CS50 assessment routine still accepted the code).
Hope that helped :)

yes, thanx a lot! very helpful =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment