Skip to content

Instantly share code, notes, and snippets.

@emberian
Forked from devyn/gist:2410724
Created April 18, 2012 02:51
Show Gist options
  • Save emberian/2410759 to your computer and use it in GitHub Desktop.
Save emberian/2410759 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct
{ unsigned long length
; char *data
;
} String;
String* String_shell ()
{ return malloc (sizeof (long) + sizeof (char*))
;
}
String* String_new (unsigned long length)
{ String* out = String_shell ()
; out->data = malloc (sizeof (char) * length)
; out->length = 0
; return out
;
}
void String_reverse (String* source, String* dest)
{ unsigned long len = dest->length = source->length
; unsigned long i
; for ( i = 0
; i < len
; i++
)
dest->data [i] = source->data [len - i - 1]
;
}
void String_print (String* str)
{ fwrite (str->data, str->length, 1, stdout)
;
}
int main (int argc, char** argv)
{ if (argc > 1)
{ unsigned long len = strlen (argv[1])
; String* in = String_new (len)
; String* out = String_new (len)
; in->length = len
; in->data = argv[1]
; String_reverse (in, out)
; String_print (out)
; fputs ("\n", stdout)
; return 0
;
}
else
{ fputs ("Not enough arguments.\n", stderr)
; return 1
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment