#include "string_func.h"

#define NULL (void*)0

int strlength(const char *string)
{
	const char *p = string;
	while (*p != '\0')
		p++;
	return p - string;
}

int strcomp(const char *s1,const char *s2)
{
	while (*s1 == *s2)
	{
		if (*s1 == '\0')
			return 0;
		s1++;
		s2++;
	}
	return *s1 - *s2;
}

char *strstring(const char *haystack,const char *needle)
{
	const char *p;
	const char *q;
	while (*haystack != '\0')
	{
		p = haystack;
		q = needle;
		while (*q == *p)
		{
			if (*q == '\0')
				return (char*)haystack;
			q++;
			p++;
			
		}
		if (*q == '\0')
			return (char*)haystack;
		haystack++;
	}
	return NULL;
}

char *strcharacter(const char *string,char c)
{
	while (*string != c)
		string++;
	return (char*)string;
}

char *parse_until(const char *src,char *dest,char until)
{
	char *p = dest;
	while ((*dest = *src) != until)
	{
		src++;
		dest++;
	}
	*dest = '\0';
	return p;
}

char *reverse(char string[])
{
	for (int i = 0 , j = strlength(string) - 1 ; i < j ; i++ , j--)
	{
		char temp = string[i];
		string[i] = string[j];
		string[j] = temp;
	}
	return &string[0];
}
