Skip to content

Instantly share code, notes, and snippets.

View radiofreejohn's full-sized avatar
🤓

John Clover radiofreejohn

🤓
View GitHub Profile
@radiofreejohn
radiofreejohn / rita.c
Created March 11, 2011 07:50
K&R exercise 4-12, recursive itoa attempt
#include <stdio.h>
#include <limits.h>
// K&R 4-12
// char *rita seems OK too but I don't want to return a char to the original caller
// not sure if this is bad practice.
// will overflow if integer is longer than the target array (which shouldn't happen)
void *rita(char *target, long source)
{
@radiofreejohn
radiofreejohn / kr5-3.c
Created March 12, 2011 05:33
K&R exercise 5-3, strcat
#include <stdio.h>
void *strcatsnot(char *s, const char *t);
void *strcatsnot(char *s, const char *t)
{
while (*++s)
;
while (*s++ = *t++)
;
@radiofreejohn
radiofreejohn / kr5-4.c
Created March 12, 2011 22:56
K&R exercise 5-4, strend, find string at end of another string
#include <stdio.h>
int strend(char *s, char *t);
int strend(char *s, char *t)
{
//save pointer to the first element of t
char *start = t;
//move to the end of each string
while (*++s)
@radiofreejohn
radiofreejohn / kr5-5-strncat.c
Created March 12, 2011 23:31
K&R Exercise 5-5, strncat
#include <stdio.h>
void strnfeline(char *s, const char *t, int n);
void strnfeline(char *s, const char *t, int n)
{
//this is bad if s is not terminated
while(*++s)
;
while(n--)
@radiofreejohn
radiofreejohn / kr5-10.c
Created March 18, 2011 08:12
K&R Exercise 5-10, reverse polish notation evaluator, this is rough
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
float polishStack[100];
float *stackPtr;
float tempx, tempy;
float *start;
@radiofreejohn
radiofreejohn / function-pointers.c
Created March 18, 2011 23:51
experimenting with function pointers, what a mess...
#include <stdio.h>
void printnnl(char *);
void printnl(char *);
void printgeneric(void (*printfunc)(char *), char *str);
void printgeneric(void (*printfunc)(char *), char *str)
{
(*printfunc)(str);
}
@radiofreejohn
radiofreejohn / range.c
Created March 23, 2011 04:27
generate a range of integer values range(start,end) - may be neg to pos and vice versa
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int *range(int, int);
int *range(int start, int end)
{
int size = (int) fabs(end-start)+1;
int *values = malloc(size * sizeof(int));
@radiofreejohn
radiofreejohn / randword.c
Created April 4, 2011 02:52
open a dictionary file and return a random word
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
/*
I wrote this as a sort of challenge, I wanted to pass random words into the K&R exercise
that generates a hash table of key value pairs to see how quickly the hashes stack up on
one another. I could have easily piped random words to the input, but that's not painful
at all...
*/
@radiofreejohn
radiofreejohn / numbers.c
Created April 9, 2011 23:02
structure and functions to generate ranges of integers
#include <math.h>
#include <stdlib.h>
#include <time.h>
struct numbers {
unsigned int size;
int *values;
};
struct Set {
const unsigned int size;
const int *values;
@radiofreejohn
radiofreejohn / heap.c
Created April 9, 2011 23:42
heapSort with priority queue and test program
/*
heapSort example using priority queue from Sedgewick's Algorithms in C++
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "numbers.h" // https://gist.github.com/911872
void insert(int);
int hremove(void);