Skip to content

Instantly share code, notes, and snippets.

View EdgeCaseBerg's full-sized avatar
📉
Wondering why Github added statuses instead of something useful

Ethan Eldridge EdgeCaseBerg

📉
Wondering why Github added statuses instead of something useful
View GitHub Profile
@EdgeCaseBerg
EdgeCaseBerg / SimpleLog.java
Created June 30, 2013 17:08
Simple singleton logging that creates logs directory wherever the process is run from. Just call SimpleLog.log( String message) ;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
//Seriously why do I have to import 3 things so get the date. Java = superflous objects everywhere
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public final class SimpleLog{
@EdgeCaseBerg
EdgeCaseBerg / counter.c
Created July 5, 2013 00:11
Using the \b escape character in C for a simple counter
#include <stdio.h>
//Compile with: $cc counter.c
//run with ./a.out
main(){
int i;
for (i = 0; i < 10; ++i){
//The \b is a lesser known escape character that moves the cursor back one. Allowing you to overwrite what you've previously written.
printf("\b%i",i);
@EdgeCaseBerg
EdgeCaseBerg / TheCProgrammingLanguage_Exercise1_9.c
Last active May 28, 2019 15:54
Exercise 1-9 from The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie​ "Write a program to copy its input to its output replacing each string of one or more blanks by a single blank"
#include <stdio.h>
main(){
int c;
while((c = getchar()) != EOF){
putchar(c);
if(c == ' '){
while((c = getchar()) == ' ')
;
putchar(c);
@EdgeCaseBerg
EdgeCaseBerg / onelineit.c
Created July 5, 2013 01:55
Simple program to take an input from the stdin and spit it to stdout on a single line without any duplicate whitespace. Not as efficient as it could be, but quick and dirty.
#include <stdio.h>
main(){
int c;
while((c = getchar()) != EOF){
if(c == '\n'){
putchar(' ');
while((c = getchar()) == '\n')
;
@EdgeCaseBerg
EdgeCaseBerg / wordlengthhisto.c
Last active March 13, 2023 20:20
Exercise 1-13 from The C Programming Language: by Brian W. Kernighan, Dennis M. Ritchie​ "Write a program to print a histogram of the lengths of words in its input. It is easy to draw the historgram with the bars horizontal; a vertical orientation is more challenging."
#include <stdio.h>
//In a word or not
#define IN 1
#define OUT 0
#define MAXLENGTH 100
// Write a program to print a histogram of the lengths of words in its input. It is easy to draw the historgram with the bars horizontal; a vertical orientation is more challenging.
main(){
//Maximum word length is 100, far more than neccesary
@EdgeCaseBerg
EdgeCaseBerg / longestline.c
Created July 5, 2013 03:40
Code Listing 1.9 from The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie​. Modified slightly to not include the newline of a line in the character sequence modified by the ourgetline function.
#include <stdio.h>
#define MAXLINE 1000
int ourgetline(char line[], int maxline);
void copy(char to[], char from[]);
main(){
int len, max;
char line[MAXLINE];
char longest[MAXLINE];
@EdgeCaseBerg
EdgeCaseBerg / reverse.c
Last active September 7, 2018 17:14
Exercise 1-19 from The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie​. "Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input line by line" Only instead of a function I've written a small program that accepts piped in input and does it there
#include <stdio.h>
#define MAXLENGTH 1000
//Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input line by line
void reverse(char s[], int lim){
int i,j;
j=lim-1;
i=0;
@EdgeCaseBerg
EdgeCaseBerg / c_comment_removal.c
Created July 6, 2013 03:23
Exercise 1-23 from The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie. Problem statement in gist. And yes, there is a goto in this code. This is an acceptable goto as a normal continue statement will not work due to the scope of the looping. If you still subscribe to religious dogmatism of the evils of a goto, then please refer …
#include <stdio.h>
/* Write a program to remove all comments from a C program,
Don't forget to handle quoted strings and character constants
properly. C Comments do not nest */
main(){
int first,second ,eatChar,lookAhead,previous;
first = getchar();
while((second = getchar()) != EOF){
@EdgeCaseBerg
EdgeCaseBerg / wrap80.c
Created July 6, 2013 04:37
Exercise 1-22 from The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie​ "Write a program to fold long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs…
#include <stdio.h>
/*
Write a program to fold long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
*/
#define COLUMN_WIDTH 80
#define MAX_LINE 1000
@EdgeCaseBerg
EdgeCaseBerg / mod2powers.c
Created July 6, 2013 05:34
Messing around with a fun fact. Since unsigned integers wrap around, you are able to do modulo arithmetic implicitly for common sizes of 2^n, where n =128 (signed chars), 256 (unsigned chars), 65536 ( short int), and etc for long doubles and other types.
#include <stdio.h>
/*
Fun Fact:
Using an unsigned integer causes integer arithmetic module 2^n where n is the number of bits in the type
*/
main(){
unsigned char mod256;
mod256 = 16*18;