Skip to content

Instantly share code, notes, and snippets.

@Jonathan-Adly
Last active June 11, 2020 16:07
Show Gist options
  • Select an option

  • Save Jonathan-Adly/2acd773b3bb7ba317f341ff6d346e5c1 to your computer and use it in GitHub Desktop.

Select an option

Save Jonathan-Adly/2acd773b3bb7ba317f341ff6d346e5c1 to your computer and use it in GitHub Desktop.
CS50 - PSET2
#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc == 2) // needs a character to start, else return error message
{
for (int j = 0; j < strlen(argv[1]); j++) // go through every character of argv 1
{
if (isdigit(argv[1][j] == ! 0)) // if its not a number
{
printf("Usage: ./caesar key\n"); //return error message
return 1;
}
else //if its a number
{
int k = atoi(argv[1]); //convert user input to an integar k // convert the string entered into integar
string plaintext = get_string("Plain text: "); //ask user for plain text
printf("ciphertext: ");
for (int i = 0; i < strlen(plaintext); i++) //go through every letter of the plain text
{
if (plaintext[i] >= 'a' && plaintext[i] <= 'z') // if smallercase, then print using the modulo equation
{
char c = ((plaintext[i] - 'a') + k) % 26 + 'a'; //covert from ascii to 0-26 scale, modulo to wrap aroud alphapet
printf("%c", c);
}
else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') //if uppercase, then print using the moduo equation
{
char C = ((plaintext[i] - 'A') + k) % 26 + 'A'; // see line 19
printf("%c", C);
}
else
{
printf("%c", plaintext[i]); // if it is not uppercase or lowercase, print as is
}
}
printf("\n");
return 0;
}
}
}
else
{
printf("Usage: ./caesar key\n"); //user puts something other than an integer print message
return 1;
}
}
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
int main(void)
{
string text = get_string("Text: "); // ask user for input
int letterscount = 0; // decalare variables
int wordcount = 1;
int sentencecount = 0;
for (int i = 0; i < strlen(text); i++) // run loop to count variables
{
if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
{
letterscount++;
}
else if (text[i] == ' ')
{
wordcount++;
}
else if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentencecount++;
}
}
float grade = 0.0588 * (100 * (float) letterscount / (float) wordcount) - 0.296 * (100 * (float) sentencecount / (float) wordcount) - 15.8;
if (grade < 16 && grade >= 0)
{
printf("Grade %i\n", (int) round(grade));
}
else if (grade >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Before Grade 1\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment