c-programming

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 1715254ba00f33007f0e55c46b6e82bb30a907f9
parent 6098c3f26b27f313ff82a1e08ea6e18056f64c33
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 26 Oct 2025 21:11:48 -0500

Completed most of chapter 1

Diffstat:
Ac-book/ch1/count-digits.c | 32++++++++++++++++++++++++++++++++
Ac-book/ch1/count-digitsv2.c | 32++++++++++++++++++++++++++++++++
Ac-book/ch1/exponentiation.c | 41+++++++++++++++++++++++++++++++++++++++++
Ac-book/ch1/word-length-histogram.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrypto/frequency-analysis.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 248 insertions(+), 0 deletions(-)

diff --git a/c-book/ch1/count-digits.c b/c-book/ch1/count-digits.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +int main(){ + int c, i, nwhite, nother; + + int ndigit[10]; + + nwhite = nother = 0; + + for (i = 0 ; i < 10; ++i){ + ndigit[i] = 0; + } + + while((c = getchar()) != EOF){ + if(c >= '0' & c <= '9'){ + ++ndigit[c-'0']; + } + else if(c == ' ' || c == '\n' || c == '\t'){ + ++nwhite; + } + else{ + ++nother; + } + } + printf("digits = "); + for(i = 0; i < 10; ++i){ + printf(" %d", ndigit[i]); + } + printf(", white space = %d, other = %d\n", nwhite, nother); + + return 0; +} diff --git a/c-book/ch1/count-digitsv2.c b/c-book/ch1/count-digitsv2.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +int main(){ + + + int digits [10]; + int countWhite, countOther; + char current; + + for(int i = 0; i < 10; ++i) + digits[i] = 0; + + while((current = getchar()) != EOF) + if (current >= '0' && current <= '9') + digits[current - '0'] += 1; + else if(current == '\t' || current == '\n' || current == ' ') + countWhite += 1; + else + countOther += 1; + + + printf("\nDigits: "); + + for(int i = 0; i < 10; ++i) + printf("%d ", digits[i]); + + printf("\nWhite Space: %d", countWhite); + printf("\nOther Characters: %d\n", countOther); + + + return 0; +} diff --git a/c-book/ch1/exponentiation.c b/c-book/ch1/exponentiation.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <stdlib.h> + +int _powerRecurse(int current, int original, int n){ + if(n == 1){ + return current; + } + else{ + return _powerRecurse(current * original, original, n - 1); + } +} + +int power(int m, int n){ + if(n == 0){ + return 1; + } + + if (n < 0){ + printf("Negative exponents not supported.\n"); + exit(-1); + } + + return _powerRecurse(m,m,n); +} + +int main(int argc, char** argv){ + + if(argc != 3){ + printf("Usage: exp {base} {exponent}\n"); + return -1; + } + + int base = atoi(argv[1]); + int exp = atoi(argv[2]); + + int result = power(base,exp); + + printf("%d^%d = %d\n", base, exp, result); + + return 0; +} diff --git a/c-book/ch1/word-length-histogram.c b/c-book/ch1/word-length-histogram.c @@ -0,0 +1,95 @@ +#include <stdio.h> + +#define IN_WORD 0 +#define OUT_WORD 1 +#define MAX_WORD_LENGTH 20 + +int main(){ + + char c; + int status = OUT_WORD; + int wordLengths [MAX_WORD_LENGTH]; + int currentLength = 0; + + for(int i = 0 ; i < MAX_WORD_LENGTH; ++i){ + wordLengths[i] = 0; + } + + while((c = getchar()) != EOF){ + if(status == OUT_WORD){ + if(c != ' ' && c != '\t' && c != '\n'){ + status = IN_WORD; + currentLength = 1; + } + } + else{ + if(c == '\t' || c == '\n' || c == ' '){ + status = OUT_WORD; + // if it's too long, just don't count it. + if (currentLength < MAX_WORD_LENGTH){ + wordLengths[currentLength] += 1; + } + + } + else{ + currentLength += 1; + } + } + + } + + if (currentLength != 0){ + // if it's too long, just don't count it. + if (currentLength < MAX_WORD_LENGTH){ + wordLengths[currentLength] += 1; + } + currentLength = 0; + } + + // horizontal print + //for(int i = 0 ; i < MAX_WORD_LENGTH; ++i){ + // printf("\n%5d: ", i); + // for(int x = 0 ; x < wordLengths[i] ; ++x){ + // printf("x"); + // } + //} + //printf("\n"); + + + + // vertical print + + int max = 0; + for(int i = 0 ; i < MAX_WORD_LENGTH; ++i){ + if(wordLengths[i] > max){ + max = wordLengths[i]; + } + } + + int current = max; + + for(int i = max ; i > 0; --i){ + printf("\n"); + for(int x = 0 ; x < MAX_WORD_LENGTH; ++x){ + if(wordLengths[x] > i){ + char p = 'x'; + printf("%3c", p); + } + else{ + char p = ' '; + printf("%3c", p); + } + } + } + printf("\n"); + + for(int x = 0 ; x < MAX_WORD_LENGTH; ++x){ + printf("%3d", x); + } + + + printf("\n"); + + + return 0; +} diff --git a/crypto/frequency-analysis.c b/crypto/frequency-analysis.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char** argv){ + if(argc != 3){ + printf("{source-file}"); + } + + char* source = argv[1]; + + FILE* fpSrc = fopen(source, "r"); + + fseek(fpSrc, 0, SEEK_END); + long fsize = ftell(fpSrc); + fseek(fpSrc, 0, SEEK_SET); + + char * fileContents = malloc(fsize + 1); + fread(fileContents, fsize, 1, fpSrc); + fclose(fpSrc); + + fileContents[fsize] = 0; + + char c; + int itr; + + itr = 0; + int charCounts [256]; + + for(int i = 0 ; i < 256; ++i){ + charCounts[i] = 0; + } + + while((c = fileContents[itr]) != 0){ + charCounts[c] += 1; + itr += 1; + } + + for(int i = 0 ; i < 256; ++i){ + if((i <= 'Z' && i >= 'A') || (i <= 'z' && i >= 'a')){ + if(charCounts[i] != 0){ + printf("%5c%5d\n", (char)i, charCounts[i]); + } + } + } + + free(fileContents); + +}