-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions1.c
executable file
·128 lines (112 loc) · 2.2 KB
/
functions1.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
#include "main.h"
/**
* _strcpy - copies a string into another
* @line_ptr: to be copied
* Return: pointer to the copied string
*/
char *_strcpy(char *line_ptr)
{
char *copied = NULL;
int i, len;
len = _strlen(line_ptr);
copied = malloc((len + 1) * sizeof(char));
if (copied == NULL)
{
return (NULL);
}
for (i = 0; i < len; i++)
{
copied[i] = line_ptr[i];
}
copied[i] = '\0';
return (copied);
}
/**
* _strcpy2 - Copies a string pointed to by @src, including
* the terminating null byte, to a buffer pointed
* to by @dest.
* @dest: A buffer to copy the string to.
* @src: The source string to copy.
*
* Return: A pointer to the @dest.
*/
char *_strcpy2(char *dest, char *src)
{
int idx;
int len = 0;
/** A loop to find the length of the string */
while (src[len] != '\0')
{
len++;
}
for (idx = 0; idx <= len; idx++)
{
dest[idx] = src[idx];
}
return (dest);
}
/**
* _strlen - calculates length of a string
* @str: to calculate its length
* Return: length of string
*/
int _strlen(char *str)
{
int len = 0;
for (; str[len] != '\0'; len++)
;
return (len);
}
/**
* _strcat - appends the src string to the dest string,
* overwriting the terminating null byte (\0)
* at the end of dest, and then adds a
* terminating null byte.
*
* @src: The source string.
* @dest: The destination string.
* Return: a pointer to the resulting string dest.
*/
char *_strcat(char *dest, char *src)
{
int didx = 0;
int sidx = 0;
/* A loop to put pointer at string end */
while (dest[didx] != '\0')
{
didx++;
}
while (src[sidx] != '\0')
{
dest[didx] = src[sidx];
didx++;
sidx++;
}
dest[didx] = '\0';
return (dest);
}
/**
* _strcmp - compares two strings.
* @s1: A pointer to the first string to be compared.
* @s2: A pointer to the second string to be compared.
*
* Return: int that tells num spaces in between
*/
int _strcmp(char *s1, char *s2)
{
if (s1 == NULL || s2 == NULL)
return (-1);
/* Make sure not at end of string */
while ((*s1 != '\0') || (*s2 != '\0'))
{
/* if elements a different, return the difference */
if (*s1 != *s2)
{
return (*s1 - *s2);
}
/* loop index */
s1++;
s2++;
}
return (0);
}