Skip to content

Commit

Permalink
alternative of strchr implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
habibaadam committed Jul 24, 2023
1 parent 1d74a38 commit a6ffb12
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 95 deletions.
2 changes: 1 addition & 1 deletion custom_strtok.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ char *alt_strtok(char *string, const char *del)
return (NULL);
}

while (*remainder != '\0' && strchr(del, *remainder) != NULL)
while (*remainder != '\0' && strchr_alt(del, *remainder) != NULL)
{
remainder++;
}
Expand Down
Binary file modified hsh
Binary file not shown.
2 changes: 1 addition & 1 deletion shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void shell_env(void);
size_t strlen_alt(const char *s);
char *strcat_alt(char *dest, const char *src);
int strcmp_alt(char *s1, char *s2);
const char *strchr_alt(const char *s, char c);
char *strchr_alt(const char *s, char c);
char *strdup_func(const char *s);
char *strncat_alt(char *dest, char *src, int n);
size_t strspn_alt(char *s, char *accept);
Expand Down
181 changes: 88 additions & 93 deletions str_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
size_t strlen_alt(const char *s);
char *strcat_alt(char *dest, const char *src);
int strcmp_alt(char *s1, char *s2);
const char *strchr_alt(const char *s, char c);
char *strchr_alt(const char *s, char c);
char *strcpy_alt(char *destination, char *source);
char *getenv_alt(const char *variable_name);
/**
*strlen_alt - Entry point
* Description - 'function returning length of a string'
* @s: string to be measured
*
* Return: the length of given string 's'
*/
*strlen_alt - Entry point
* Description - 'function returning length of a string'
* @s: string to be measured
*
* Return: the length of given string 's'
*/
size_t strlen_alt(const char *s)
{
size_t len = 0;
Expand All @@ -25,136 +25,131 @@ size_t strlen_alt(const char *s)
}

/**
* strcat_alt - Entry point
* Description - 'a function concantenating two strings'
* @dest: char string type
* @src: char string type
*
* Return: pointer to the concantenated string
*/
* strcat_alt - Entry point
* Description - 'a function concantenating two strings'
* @dest: char string type
* @src: char string type
*
* Return: pointer to the concantenated string
*/
char *strcat_alt(char *dest, const char *src)
{
int dest_len = 0;
int h;
int dest_len = 0;
int h;


/*Find the length of the destination string*/
/*Find the length of the destination string*/


while (dest[dest_len] != '\0')
{
dest_len++;
}
while (dest[dest_len] != '\0')
{
dest_len++;
}


/*append the first string to the destination string */
/*append the first string to the destination string */


for (h = 0; src[h] != '\0'; h++)
{
dest[dest_len + h] = src[h];
}
/* add null terminating character to concantenated string */
for (h = 0; src[h] != '\0'; h++)
{
dest[dest_len + h] = src[h];
}
/* add null terminating character to concantenated string */


dest[dest_len + h] = '\0';
dest[dest_len + h] = '\0';


return (dest);
return (dest);
}




/**
* strcmp_alt - Entry point
* Description - 'a function comparing two strings'
* @s1: first string to be checked
* @s2: second string to be checked
*
* Return: value less than 0 means s1 is less than s2
* value equal to 0 meants s1 = s2
* value greater than 0 means s1 is greater than s2
*/
* strcmp_alt - Entry point
* Description - 'a function comparing two strings'
* @s1: first string to be checked
* @s2: second string to be checked
*
* Return: value less than 0 means s1 is less than s2
* value equal to 0 meants s1 = s2
* value greater than 0 means s1 is greater than s2
*/


int strcmp_alt(char *s1, char *s2)
{
int len;
int len;


for (len = 0; s1[len] == s2[len] && s1[len]; len++)
;
for (len = 0; s1[len] == s2[len] && s1[len]; len++)
;


if (s1[len] > s2[len])
return (1);
if (s1[len] < s2[len])
return (-1);
return (0);
if (s1[len] > s2[len])
return (1);
if (s1[len] < s2[len])
return (-1);
return (0);
}




/**
* strchr_alt - Entry point
* Description - ' a function searching for the first occurence of a character
* in a string'
* @s: a pointer to the string to be searched
* @c: the character to be searched for
*
* Return: a pointer to the first occurrence of the char c in s
* 0R 0 if char c is not found in s
*/


const char *strchr_alt(const char *s, char c)
{
int h;
* strchr_alt - Entry point
* Description - ' a function searching for the first occurence of a character
* in a string'
* @s: a pointer to the string to be searched
* @c: the character to be searched for
*
* Return: a pointer to the first occurrence of the char c in s
* 0R 0 if char c is not found in s
*/


for (h = 0; s[h] >= 0; h++)
{
if (s[h] == c)
char *strchr_alt(const char *s, char c)
{
return (s + h);
}
}
return (NULL);
while (*s != '\0')
{
if (*s == c)
return ((char *)s);
s++;
}
return (NULL);
}


/**
* memcpy_alt - Entry point
* Description: 'function copying memory area'
* @src: source char string type
* @dest: destination string type
* @n: maximum number of byte area to be copied
* Return: pointer to destination string
*/
* memcpy_alt - Entry point
* Description: 'function copying memory area'
* @src: source char string type
* @dest: destination string type
* @n: maximum number of byte area to be copied
* Return: pointer to destination string
*/


char *memcpy_alt(char *dest, char *src, unsigned int n)
{
unsigned int h = 0;
unsigned int h = 0;


for (h = 0; h < n; h++)
{
dest[h] = src[h];
for (h = 0; h < n; h++)
{
dest[h] = src[h];


}
return (dest);
}
return (dest);
}
/**
* strcpy_alt - Entry point
* Description - 'a function copying one pointer to another pointer'
* @source: char type string
* @destination: char string type
* Return: Pointer to dest
*/
* strcpy_alt - Entry point
* Description - 'a function copying one pointer to another pointer'
* @source: char type string
* @destination: char string type
* Return: Pointer to dest
*/

char *strcpy_alt(char *destination, char *source)
{
Expand All @@ -177,17 +172,17 @@ char *strcpy_alt(char *destination, char *source)
*/
char *getenv_alt(const char *variable_name)
{
int name_length = strlen_alt(variable_name);
int name_length = strlen_alt(variable_name);

char **env;

for (env = environ; *env; ++env)
{
if (strncmp_alt(variable_name, *env, name_length)
== 0 && (*env)[name_length] == '=')
{
return (&((*env)[name_length + 1]));
}
if (strncmp_alt(variable_name, *env, name_length)
== 0 && (*env)[name_length] == '=')
{
return (&((*env)[name_length + 1]));
}
}
return (NULL);
}

0 comments on commit a6ffb12

Please sign in to comment.