c-programming

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

commit 6098c3f26b27f313ff82a1e08ea6e18056f64c33
parent 00a927d51f767a45f4e9fa752cc6ab49c830789f
Author: Andrew Laack <andrew@laack.co>
Date:   Sat, 18 Oct 2025 00:39:01 -0500

Did a bit more c programming stuff

Diffstat:
Ac-book/ch1/count-characters.c | 11+++++++++++
Ac-book/ch1/count-words.c | 34++++++++++++++++++++++++++++++++++
Ac-book/ch1/io-cp.c | 8++++++++
Ac-book/ch1/line-count.c | 11+++++++++++
Ac-book/ch1/print-eof.c | 5+++++
Ac-book/ch1/slow-cp.c | 29+++++++++++++++++++++++++++++
6 files changed, 98 insertions(+), 0 deletions(-)

diff --git a/c-book/ch1/count-characters.c b/c-book/ch1/count-characters.c @@ -0,0 +1,11 @@ +#include <stdio.h> +int main(){ + unsigned long itr = 0; + char c; + + while((c = getchar()) != EOF){ + itr += 1; + } + + printf("\n%ld\n", itr); +} diff --git a/c-book/ch1/count-words.c b/c-book/ch1/count-words.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#define IN_WORD 1 +#define OUT_OF_WORD 0 + +int isNotChar(int c){ + if (c == ' ' || c == '\t' || c == '\n'){ + return 0; + } + + return 1; +} + + +int main(){ + int c; + int words = 0; + int wordArea = OUT_OF_WORD; + + while((c = getchar()) != EOF){ + if (wordArea == IN_WORD){ + if(isNotChar(c) == 0){ + wordArea = OUT_OF_WORD; + } + } + else{ + if(isNotChar(c) == 1){ + wordArea = IN_WORD; + words += 1; + } + } + } + + printf("\nWord Count: %d\n", words); +} diff --git a/c-book/ch1/io-cp.c b/c-book/ch1/io-cp.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +int main(){ + int c; + while((c = getchar()) != EOF){ + putchar(c); + } +} diff --git a/c-book/ch1/line-count.c b/c-book/ch1/line-count.c @@ -0,0 +1,11 @@ +#include <stdio.h> +int main(){ + int c; + long count; + while((c = getchar()) != EOF){ + if(c == '\n'){ + count += 1; + } + } + printf("\nThere were %ld lines\n", count + 1); +} diff --git a/c-book/ch1/print-eof.c b/c-book/ch1/print-eof.c @@ -0,0 +1,5 @@ +#include <stdio.h> +int main(){ + int val = EOF; + printf("%d\n", val); +} diff --git a/c-book/ch1/slow-cp.c b/c-book/ch1/slow-cp.c @@ -0,0 +1,29 @@ +#include <stdio.h> + +int main(int argc, char** argv){ + + if(argc != 3){ + printf("Usage: slow-cp {source} {destination}"); + return -1; + } + + char* source; + char* destination; + + source = argv[1]; + destination = argv[2]; + + + FILE* file = fopen(source, "r"); + FILE* fdest = fopen(destination, "w"); + + int c; + + while((c = getc(file)) != EOF){ + fputc(c,fdest); + } + + printf("File copied successfully\n"); + + return 0; +}