forked from zguaidh/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_functions2.c
96 lines (85 loc) · 1.54 KB
/
string_functions2.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
#include "main.h"
/**
*_strstr - locate a substring withing a string
*
*@haystack: the string to be searched
*@needle: the sbstring to be located
*
*Return: a pointer to the first occurence of the sbstring or otherwise NULL
*/
char *_strstr(char *haystack, char *needle)
{
int i, j;
for (i = 0; haystack[i] != '\0'; i++)
{
for (j = 0; needle[j] != '\0'; j++)
{
if (haystack[i + j] != needle[j])
break;
}
if (!needle[j])
return (&haystack[i]);
}
return (NULL);
}
/**
*_strcat - concatenate two strings to create a new string
*
*@s1: the first string to be concatenated
*@s2: the second string to be concatenated
*
*Return: a pointer to newly created concatenated string
*/
char *_strcat(char *s1, char *s2)
{
int a = 0, b = 0;
while (s1[a] != '\0')
a++;
while (s2[b] != '\0')
{
s1[a] = s2[b];
a++;
b++;
}
s1[a] = '\0';
return (s1);
}
/**
*print_char - prints a single character to stdout
*@c: the character to be printed
*
*Return: the number of bytes written
*/
int print_char(char c)
{
return (write(STDERR_FILENO, &c, 1));
}
/**
*_print_err - prints Error message to the stdout
*@str: a string containing the error message
*
* Return: no return value
*/
void _print_err(char *str)
{
write(STDERR_FILENO, str, _strlen(str));
}
/**
* _strcpy - copies contents from s2 onto s1
*
* @s1: destination string
* @s2: source string
*
* Return: returns string s1
*/
char *_strcpy(char *s1, char *s2)
{
int i = 0;
s1[i] = s2[i];
while (s2[i] != '\0')
{
i++;
s1[i] = s2[i];
}
return (s1);
}