1.String length implementation
size_t my_strlen(const char* str)
{
if (str == NULL)
{
return 0;
}
int length = 0;
const char *ch = str;
while (*ch != '\0')
{
length++;
ch++;
}
return length;
}
2.String character check implementation
size_t my_strchar(const char* str, int c)
{
if (str == NULL)
{
return 0;
}
while (*str != '\0')
{
if (*str == c)
{
return (char*) str;
}
str++;
}
return NULL;
}
3.Modifications to pointers
int value = 10;
int *int_ptr = & value;
some_function(int_ptr);
printf("%d\n", value);
void some_function(int* some_ptr)
{
*some_ptr = 11;
}
4.Reassignment of pointers
int value = 10;
int *int_ptr = & value;
some_function(int_ptr);
printf("%d\n", value);
void some_function(int* some_ptr)
{
some_ptr = (int*) malloc (sizeof(int));
*some_ptr = 11;
}
5.A function to check for a string in a string
char* my_strstr(const char* heystack, const char* needle)
{
if (heyStack == NULL || needle == NULL)
{
return NULL;
}
while (*heystack != '\0')
{
if (*heystack == *needle)
{
const char* h = heystack;
const char* n = needle
while (*n != '\0' && *h == *n)
{
h++;
n++;
}
if (*n == '\0')
{
return (char*) heyStack;
}
}
heystack++;
}
return NULL;
}
6.A function to compare two strings
int my_strcmp(const char* str1, const char* str2)
{
if (str1 == NULL && str2 == NULL)
{
return 0;
}
if (str1 == NULL)
{
return 0 - *str2;
}
if (str2 == NULL)
{
return *str1;
}
const char* ch1 = str1;
const char* ch2 = str2;
while (*ch1 == '\0' && *ch2 == '\0')
{
if (*ch1 != *ch2)
{
return *ch1 - *ch2;
}
ch1++;
ch2++;
}
if (*ch1 == '\0' && *ch2 != '\0')
{
return 0 - *str2;
}
if (*ch1 != '\0' && *ch2 == '\0')
{
return *str1;
}
}
7. A function to concatenate strings
char* my_strcat(char* dest, const char* src)
{
if (dest == NULL || src == NULL)
{
return dest;
}
chasr* d = dest;
while (*d != '\0')
{
d++;
}
while (*src != '\0')
{
*d = *src;
d++;
src++;
}
*d = '\0';
return NULL;
}