-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_helper_functions.c
101 lines (90 loc) · 1.65 KB
/
string_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
#include "main.h"
/**
* _strlen - string length
* @str: array of charahcters null terminated
* Return: length of string
*/
int _strlen(char *str)
{
int i = 0;
while (str && str[i] != '\0')
i++;
return (i);
}
/**
* _strcmp - compared two strings
* @str1: first one
* @str2: seocnd one
* Return: (0) match (1) not match
*/
int _strcmp(char *str1, char *str2)
{
int pos = 0;
while (str1[pos])
{
if (str1[pos] != str2[pos])
return (1);
pos++;
}
return (0);
}
/**
* _strcopy - copy str to new allocated one and return it
* @str: source
* Return: newly allocated string copy of source
*/
char *_strcopy(char *str)
{
int length, i = 0;
char *strcopy = NULL;
length = _strlen(str);
/* 1 to null character */
strcopy = malloc(length + 1);
while (i < length && str[i])
{
strcopy[i] = str[i];
i++;
}
strcopy[i] = '\0';
return (strcopy);
}
/**
* _strconcat - add str2 command to str1 path
* @str1: first string
* @str2: second string
* Return: str1 after addition
*/
char *_strconcat(char *str1, char *str2)
{
int str1_length, str2_length, i;
char *full_str = NULL;
i = 0;
str1_length = _strlen(str1);
str2_length = _strlen(str2);
/* 2 for / , \0 chars */
full_str = malloc(str1_length + str2_length + 2);
for (i = 0; i < str1_length; i++)
full_str[i] = str1[i];
full_str[i] = '/';
for (i = 0; i < str2_length; i++)
full_str[(i + str1_length)+1] = str2[i];
full_str[str1_length + str2_length + 1] = '\0';
return (full_str);
}
/**
* remove_comment - remove comment # from string
* @str: string from stander input
*/
void remove_comment(char *str)
{
int i = 0;
while (str && str[i] != '\0')
{
if (str[i] == ' ' && str[i + 1] == '#')
{
str[i] = '\0';
break;
}
i++;
}
}