Initial commit with cleaned history

This commit is contained in:
2024-09-01 18:09:25 +02:00
commit 208004e9d0
273 changed files with 234697 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
// Explicitly casts chars to ints
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("String: ");
for (int i = 0; i < strlen(s); i++)
{
int c = (int) s[i];
printf("%c %i\n", s[i], c);
}
}
+14
View File
@@ -0,0 +1,14 @@
// Implicitly casts chars to ints
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("String: ");
for (int i = 0; i < strlen(s); i++)
{
printf("%c %i\n", s[i], s[i]);
}
}
+11
View File
@@ -0,0 +1,11 @@
// Buggy example for printf
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 3; i++)
{
printf("#\n");
}
}
+24
View File
@@ -0,0 +1,24 @@
// Buggy example for debug50
#include <cs50.h>
#include <stdio.h>
int get_negative_int(void);
int main(void)
{
int i = get_negative_int();
printf("%i\n", i);
}
// Prompt user for positive integer
int get_negative_int(void)
{
int n;
do
{
n = get_int("Negative Integer: ");
}
while (n < 0);
return n;
}
+10
View File
@@ -0,0 +1,10 @@
// Uses get_string
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string answer = get_string("What's your name? ");
printf("hello, %s\n", answer);
}
+10
View File
@@ -0,0 +1,10 @@
// Uses get_string with better-named variable
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string name = get_string("What's your name? ");
printf("hello, %s\n", name);
}
+16
View File
@@ -0,0 +1,16 @@
// Prints a command-line argument
#include <cs50.h>
#include <stdio.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
printf("hello, %s\n", argv[1]);
}
else
{
printf("hello, world\n");
}
}
+12
View File
@@ -0,0 +1,12 @@
// Prints command-line arguments
#include <cs50.h>
#include <stdio.h>
int main(int argc, string argv[])
{
for (int i = 0; i < argc; i++)
{
printf("%s\n", argv[i]);
}
}
+6
View File
@@ -0,0 +1,6 @@
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
+8
View File
@@ -0,0 +1,8 @@
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string name = get_string("What's your name? ");
printf("hello, %s\n", name);
}
+12
View File
@@ -0,0 +1,12 @@
// Prints chars
#include <stdio.h>
int main(void)
{
char c1 = 'H';
char c2 = 'I';
char c3 = '!';
printf("%c%c%c\n", c1, c2, c3);
}
+12
View File
@@ -0,0 +1,12 @@
// Prints chars' ASCII codes
#include <stdio.h>
int main(void)
{
char c1 = 'H';
char c2 = 'I';
char c3 = '!';
printf("%i %i %i\n", c1, c2, c3);
}
+10
View File
@@ -0,0 +1,10 @@
// Prints string
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = "HI!";
printf("%s\n", s);
}
+10
View File
@@ -0,0 +1,10 @@
// Prints string's ASCII codes
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = "HI!";
printf("%i %i %i\n", s[0], s[1], s[2]);
}
+10
View File
@@ -0,0 +1,10 @@
// Prints string's ASCII codes, including NUL
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = "HI!";
printf("%i %i %i %i\n", s[0], s[1], s[2], s[3]);
}
+13
View File
@@ -0,0 +1,13 @@
// Multiple strings
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = "HI!";
string t = "BYE!";
printf("%s\n", s);
printf("%s\n", t);
}
+15
View File
@@ -0,0 +1,15 @@
// Array of strings
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string words[2];
words[0] = "HI!";
words[1] = "BYE!";
printf("%s\n", words[0]);
printf("%s\n", words[1]);
}
+14
View File
@@ -0,0 +1,14 @@
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string words[2];
words[0] = "HI!";
words[1] = "BYE!";
printf("%c%c%c\n", words[0][0], words[0][1], words[0][2]);
printf("%c%c%c%c\n", words[1][0], words[1][1], words[1][2], words[1][3]);
}
+18
View File
@@ -0,0 +1,18 @@
// Determines the length of a string
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt for user's name
string name = get_string("Name: ");
// Count number of characters up until '\0' (aka NUL)
int n = 0;
while (name[n] != '\0')
{
n++;
}
printf("%i\n", n);
}
+25
View File
@@ -0,0 +1,25 @@
// Determines the length of a string using a function
#include <cs50.h>
#include <stdio.h>
int string_length(string s);
int main(void)
{
// Prompt for user's name
string name = get_string("Name: ");
int length = string_length(name);
printf("%i\n", length);
}
int string_length(string s)
{
// Count number of characters up until '\0' (aka NUL)
int n = 0;
while (s[n] != '\0')
{
n++;
}
return n;
}
+13
View File
@@ -0,0 +1,13 @@
// Determines the length of a string using a function
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// Prompt for user's name
string name = get_string("Name: ");
int length = strlen(name);
printf("%i\n", length);
}
+14
View File
@@ -0,0 +1,14 @@
// Averages three (hardcoded) numbers
#include <stdio.h>
int main(void)
{
// Scores
int score1 = 72;
int score2 = 73;
int score3 = 33;
// Print average
printf("Average: %f\n", (score1 + score2 + score3) / 3.0);
}
+16
View File
@@ -0,0 +1,16 @@
// Averages three (hardcoded) numbers using an array
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Scores
int scores[3];
scores[0] = 72;
scores[1] = 73;
scores[2] = 33;
// Print average
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
}
+16
View File
@@ -0,0 +1,16 @@
// Averages three numbers using an array
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Get scores
int scores[3];
scores[0] = get_int("Score: ");
scores[1] = get_int("Score: ");
scores[2] = get_int("Score: ");
// Print average
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
}
+17
View File
@@ -0,0 +1,17 @@
// Averages three numbers using an array and a loop
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Get scores
int scores[3];
for (int i = 0; i < 3; i++)
{
scores[i] = get_int("Score: ");
}
// Print average
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
}
+35
View File
@@ -0,0 +1,35 @@
// Averages three numbers using an array, a constant, and a helper function
#include <cs50.h>
#include <stdio.h>
// Constant
const int N = 3;
// Prototype
float average(int length, int array[]);
int main(void)
{
// Get scores
int scores[N];
for (int i = 0; i < N; i++)
{
scores[i] = get_int("Score: ");
}
// Print average
printf("Average: %f\n", average(N, scores));
}
float average(int length, int array[])
{
// Calculate average
int sum = 0;
for (int i = 0; i < length; i++)
{
sum += array[i];
}
return sum / (float) length;
}
+15
View File
@@ -0,0 +1,15 @@
// Returns explicit value from main
#include <cs50.h>
#include <stdio.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Missing command-line argument\n");
return 1;
}
printf("hello, %s\n", argv[1]);
return 0;
}
+16
View File
@@ -0,0 +1,16 @@
// Prints string char by char, using strlen
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Input: ");
printf("Output: ");
for (int i = 0; i < strlen(s); i++)
{
printf("%c", s[i]);
}
printf("\n");
}
+16
View File
@@ -0,0 +1,16 @@
// Prints string char by char, using strlen, remembering string's length
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Input: ");
printf("Output: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
printf("%c", s[i]);
}
printf("\n");
}
+23
View File
@@ -0,0 +1,23 @@
// Uppercases a string
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
{
printf("%c", s[i] - 32);
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
}
+24
View File
@@ -0,0 +1,24 @@
// Uppercases string using ctype library (and an unnecessary condition)
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
if (islower(s[i]))
{
printf("%c", toupper(s[i]));
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
}
+17
View File
@@ -0,0 +1,17 @@
// Uppercases string using ctype library
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
printf("%c", toupper(s[i]));
}
printf("\n");
}
+82
View File
@@ -0,0 +1,82 @@
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
const int BITS_IN_BYTE = 8;
int main(void)
{
string text = get_string("Text: ");
// Loop over every character
int i = 0;
int l = 0;
double rest = 0;
double zahl;
int len = 0;
int bi;
int n = 0;
int m = 0;
for (i = 0, len = strlen(text); i < len; i++)
{
zahl = text[i];
bi = 0;
for (l = 0; l < BITS_IN_BYTE; l++)
{
zahl = (zahl - (rest * pow(10, -1))) / 2.0 ;
rest = (zahl - floor(zahl)) * 10;
if (rest > 0)
{
bi = bi + pow(10, l);
}
else
{
continue;
}
}
if (bi >= 1000000 && bi <= 1111111)
{
char test[bi];
sprintf(test, "0%d", bi);
//printf("%s",test);
for (n = 0, m = strlen(test); n < m; n++)
{
if (test[n] == 48)
{
// Dark emoji
printf("\U000026AB");
}
else if (test[n] == 49)
{
// Light emoji
printf("\U0001F7E1");
}
}
}
else if (bi >= 100000 && bi <= 111111)
{
char test[bi];
sprintf(test, "00%d", bi);
//printf("%s",test);
for (n = 0, m = strlen(test); n < m; n++)
{
if (test[n] == 48)
{
// Dark emoji
printf("\U000026AB");
}
else if (test[n] == 49)
{
// Light emoji
printf("\U0001F7E1");
}
}
}
printf("\n");
}
}
@@ -0,0 +1,55 @@
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main(void)
{
// Get Text
string text = get_string("Text: ");
// Letter, word, sentence count
double lettercount = 0;
double wordcount = 1;
double sentencecount = 0;
for (int i = 0, len = strlen(text); i < len; i++)
{
if ((text[i] >= 65 && text[i] <= 90) || (text[i] >= 97 && text[i] <= 122))
{
lettercount += 1;
}
else if (text[i] == 32)
{
wordcount += 1;
}
else if (text[i] == 33 || text[i] == 63 || text[i] == 46)
{
sentencecount += 1;
}
else
{
continue;
}
}
// Calculate Index
double L = lettercount / wordcount * 100.0;
double S = sentencecount / wordcount * 100.0;
double index = 0.0588 * L - 0.296 * S - 15.8;
// Print conditions
if (index <= 1)
{
printf("Before Grade 1\n");
}
else if (index >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", (int) round(index));
}
}
+53
View File
@@ -0,0 +1,53 @@
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
int compute_score(string word);
int main(void)
{
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
// Score both words
int score1 = compute_score(word1);
int score2 = compute_score(word2);
// TODO: Print the winner
if (score1 > score2)
{
printf("Player 1 wins!");
}
else if (score1 < score2)
{
printf("Player 2 wins!");
}
else
{
printf("Tie!");
}
}
int compute_score(string word)
{
// TODO: Compute and return score for string
int score = 0;
for (int i = 0, len = strlen(word); i < len; i++)
{
if (isupper(word[i]))
{
score += POINTS[word[i] - 'A'];
}
else if (islower(word[i]))
{
score += POINTS[word[i] - 'a'];
}
}
return score;
}