#include <stdio.h>
#define _DEBUG_ 1
#define POOLSIZE 10000
static char pool[POOLSIZE];
static char *pool_ptr = pool;
char *myalloc(int);
void myfree(char *);
int mystrlen(char *);
void mystrcpy(char *, char *);
int mystrcmp(char *, char *);
char *mycat(const char *, char *);
int mystrend(char *, char);
char *mystrncpy(char *,char*,int);
int mystrncmp(const char *,const char *,int);
char *mystrncat(const char *, char *, int);
int myatoi(const char *, int *);
int myitoa(char *, int);
void swap(char *, char *);
char *myreverse(char *);
int mystrindex(const char *, char);
int main(int argc, char* argv[])
{
char *ptr = myalloc(10);
ptr[0] = 'H';
ptr[1] = 'i';
ptr[2] = ' ';
ptr[3] = '\0';
char *p2 = myalloc(10);
p2[0] = 's';
p2[1] = 'i';
p2[2] = 'r';
p2[3] = '.';
p2[4] = '\0';
printf("%s%s %d\n", ptr, p2, mystrlen(ptr));
char cmp1[] = "Hello";
char cmp2[] = "Hello";
char cmp3[] = "Hello!";
printf("strcmp(%s,%s) == %d\n", cmp1, cmp2, mystrcmp(cmp1,cmp2));
printf("strcmp(%s,%s) == %d\n", cmp1, cmp3, mystrcmp(cmp1,cmp3));
if(mystrend("Hello sir", 'r'))
printf("'r' is in fact at the end of Hello Sir\n");
else
printf("What?\n");
char cpyt[32];
char *cpyf = "Hi! to you sir.";
mystrncpy(cpyf, cpyt, 3);
cpyt[3] = '\0';
printf("Copying the first three elements: %s\n", cpyt);
if(mystrncmp("abcccc", "abcddd",3) == 0)
printf("mystrncmp passes test 1!\n");
if(mystrncmp("abeccc", "abcddd",3) == 1)
printf("mystrncmp passes test 2!\n");
char foo[32] = "Hello ";
mystrncat("World is going to explode!", foo, 5);
printf("strncat: %s\n", foo);
int val = 0;
if(myatoi("15", &val) == 15)
printf("myatoi passed %d\n", val);
char numbuf[32];
myitoa(numbuf, 3242);
printf("numbuf: %s\n", numbuf);
char reverseme[] = "Hello!!!";
myreverse(reverseme);
printf("reverse; %s\n", reverseme);
printf("index of 'i' in 'Hi' is %d\n", mystrindex("Hi", 'i'));
return 0;
}
int mystrindex(const char *s, char c)
{
char *front = s;
while(*s++)
if(*s == c)
return s - front;
return -1;
}
int pow(int a, int b){
int oa = a;
a = 1;
if(b == 0)
return 1;
while(b--)
a *= oa;
return a;
}
int myatoi(const char *s, int *val)
{
int n = 0;
do{
if(*s >= '0' && *s <= '9'){
n *= 10;
n += *s - '0';
s++;
}
else
return 0;
}while(*s);
*val = n;
return *val;
}
void swap(char *a, char *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
char *myreverse(char *c)
{
char *front = c;
while(*c)
c++;
c--;
while(front < c){
swap(front, c);
c--;
front++;
}
return c;
}
int myitoa(char *dest, int n)
{
char *front = dest;
if(n < 0)
*dest++ = '-';
do{
*dest++ = (n % 10) + '0';
n /= 10;
}while(n);
*dest = '\0';
myreverse(front);
return dest-front + (front[0] == '-') ? 1 : 0;
}
char *mystrncat(const char *s, char *dest, int n)
{
const char *cp = s;
while(*dest++); /* reach the end of dest */
while(n >= cp-s && dest++, (*(dest-2) = *cp++));
*dest = '\0';
return dest;
}
int mystrncmp(const char *a, const char *b, int n)
{
const char *cmpp = a; /*comparison ptr*/
while(n > cmpp - a && *a && *b)
if(*cmpp++ != *b++)
return 1;
return 0;
}
char *mystrncpy(char *src, char *dest, int n)
{
char *cp = src; /*copy ptr*/
while(n > cp-src && *src){
*dest++ = *cp++;
}
return dest;
}
int mystrend(char *target, char c)
{
while(*target++);
if(c == *(target-2))
return 1;
return 0;
}
char *mycat(const char *str, char *dest)
{
char *p = dest;
while(*(dest))
++dest;
while((*(dest++) = *(str++)));
return p;
}
int mystrcmp(char *a, char *b)
{
while(*a && *a++ == *b++);
if(*a == *b)
return 0;
return 1;
}
void mystrcpy(char *src, char *dest)
{
while((*dest++ = *src++));
}
int mystrlen(char *s)
{
char *start = s;
while(*s++);
return s - start;
}
char *myalloc(int size)
{
if(pool + POOLSIZE - pool_ptr >= size){
pool_ptr += size;
return pool_ptr - size;
}
return 0;
}
void myfree(char *target)
{
if(target >= pool && target < pool + POOLSIZE)
pool_ptr = target;
}