Skip to content

Instantly share code, notes, and snippets.

View zhoukuo's full-sized avatar

Dennis Zhou zhoukuo

View GitHub Profile
@zhoukuo
zhoukuo / strupr.c
Created July 13, 2012 03:52
convert string to uppercase
/* use toupper() to convert string */
#include <ctype.h>
char* strupr(char *s)
{
char *d = s;
while(*d)
*d++ = toupper(*d);
return s;
}
@zhoukuo
zhoukuo / strlwr.c
Created July 13, 2012 03:51
convert string to lowercase
/* use tolower() to convert string */
#include <ctype.h>
char* strlwr(char *s)
{
char *d = s;
while(*d)
*d++ = tolower(*d);
return s;
}
@zhoukuo
zhoukuo / cost_s.c
Created July 13, 2012 03:29
Calculate the execution time of second
/* use time() to calculate the execution time in second */
#include <time.h>
long timecost(void (*dosomething)())
{
time_t start, end;
long cost;
start = time(NULL);
dosomething();
end = time(NULL);
@zhoukuo
zhoukuo / cost_us.c
Created July 13, 2012 03:21
Calculate the execution time of microseconds
/* use gettimeofday() to calculate the execution time in microseconds */
#include <sys/time.h>
long timecost(void (*dosomething)())
{
struct timeval start, end;
long cost;
gettimeofday(&start, NULL);
dosomething();
gettimeofday(&end, NULL);
@zhoukuo
zhoukuo / cost_ms.c
Created July 13, 2012 03:05
Calculate the execution time of millisecond
/* use ftime() to calculate the execution time of millisecond */
#include <sys/timeb.h>
long timecost(void (*dosomething)())
{
struct timeb begin, end;
long cost;
ftime(&begin);
dosomething();
ftime(&end);
@zhoukuo
zhoukuo / byte_order.c
Created July 10, 2012 09:32
Big-endian or little-endian
/* (char)i ? printf("l\n") : printf("b\n"); */
#include <stdio.h>
int byteorder(void)
{
int i = 0x0001;
(char)i ? printf("l\n") : printf("b\n");
return i;
}