-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.c
146 lines (130 loc) · 2.4 KB
/
helper_functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "main.h"
/**
* _strlen - Entry point
* Description: 'find the string lenght'
*
* @str: 'string'
* Return: lenght of string
*/
int _strlen(char *str)
{
int i = 0;
if (str == NULL)
str = ""; /* end if */
while (*str != '\0')
{
i++;
str++;
} /* end while */
return (i);
} /* End function */
/**
* _strcpy - Entry point
* Description: 'copy strings from src to dest'
*
* @dest: 'destination'
* @src: 'source'
* Return: copied string
*/
char *_strcpy(char *dest, char *src)
{
int i = 0;
while (i <= _strlen(src))
{
dest[i] = src[i];
i++;
} /* end while */
return (dest);
} /* end function */
/**
* _strcat - Entry poiont
* Description: 'concatenate string'
*
* @dest: 'destination'
* @src: 'source'
* Return: concartenated string
*/
char *_strcat(char *dest, char *src)
{
int i = 0;
char *temp = dest;
while (*temp != '\0')
{
temp++;
} /* end while */
while (i < _strlen(src))
{
*temp = src[i];
temp++;
i++;
} /* end while */
*temp = '\0';
return (dest);
} /* end function */
/**
* _strdup - Entry point
* Description: 'duplicate a string'
*
* @str: 'string'
* Return: returns a pointer to a newly allocated memory
* block containing a duplicate of the input string
*/
char *_strdup(char *str)
{
char *myStr;
int i = 0;
if (str == NULL)
{
return (NULL);
} /* end if */
myStr = (char *)malloc(strlen(str) * sizeof(char) + 1);
if (myStr == NULL)
{
return (NULL);
} /* end if */
while (i <= (int)strlen(str))
{
myStr[i] = str[i];
i++;
} /* end if */
return (myStr);
} /* end function */
/**
* _strcmp - Entry point
* Description: 'compares two strings'
*
*
* @str1: 'string 1'
* @str2: 'stri'
*
* Return: 'If str1 is lexicographically less than str2,'
* 'strcmp returns a negative integer value (usually -1).'
* 'If str1 is lexicographically greater than str2,'
* 'strcmp returns a positive integer value (usually 1).'
* 'If str1 is lexicographically equal to str2,'
* 'strcmp returns zero.'
*/
int _strcmp(char *str1, char *str2)
{
int i = 0;
int len1, len2;
if (str1 == NULL)
str1 = ""; /* end if */
if (str2 == NULL)
str2 = ""; /* end if */
len1 = _strlen(str1);
len2 = _strlen(str2);
if (len1 > len2)
return (1); /* end if */
else if (len1 < len2)
return (-1); /* end elif */
while (i < len1)
{
if (str1[i] != str2[i])
{
return (-1);
} /* end if */
i++;
} /* end while */
return (0);
} /* end function */