-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrFuncs.c
141 lines (135 loc) · 2.11 KB
/
strFuncs.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
#include "monty.h"
/**
* fixSpace - fix spaces in statments
* @arrs: array of strings.
* Return: newly fixed array
**/
char **fixSpace(char **arrs)
{
char **arrnos;
int i = 0;
int len = globaldata->linecount;
arrnos = malloc(sizeof(char *) * globaldata->linecount);
debugMemArr(arrnos);
for (i = 0; i < len; i++)
arrnos[i] = strCatNoS("", arrs[i]);
freeArr(arrs);
return (arrnos);
}
/**
* strCatNoS - Concatinates two strings with manual allocation without spaces.
* @str1: first string.
* @str2: second string.
* Return: pointer to the newly created string
**/
char *strCatNoS(const char *str1, const char *str2)
{
size_t s1, s2, s3, i = 0;
char *a;
s1 = strLenNoS(str1);
s2 = strLenNoS(str2);
s3 = s1 + s2 + 1;
if (s3 == 1)
return (NULL);
a = malloc(s3);
debugMem(a);
while (*str1 != '\0')
{
if (*str1 != ' ')
{
a[i] = *str1;
i++;
}
else if (*str1 == ' ' && *(str1 - 1) != ' ' && *(str1 - 1) != '\0')
{
a[i] = *str1;
i++;
}
str1++;
}
while (*str2 != '\0')
{
if (*str2 != ' ')
{
a[i] = *str2;
i++;
}
else if (*str2 == ' ' && *(str2 - 1) != ' ' && *(str2 - 1) != '\0')
{
a[i] = *str2;
i++;
}
str2++;
}
a[i] = '\0';
return (a);
}
/**
* strCat - Concatinates two strings with manual allocation.
* @str1: first string.
* @str2: second string.
* Return: pointer to the newly created string
**/
char *strCat(const char *str1, const char *str2)
{
size_t s1, s2, s3, i = 0;
char *a;
s1 = strLen(str1);
s2 = strLen(str2);
s3 = s1 + s2 + 1;
if (s3 == 1)
return (NULL);
a = malloc(s3);
debugMem(a);
while (*str1 != '\0')
{
a[i] = *str1;
str1++;
i++;
}
while (*str2 != '\0')
{
a[i] = *str2;
str2++;
i++;
}
a[i] = '\0';
return (a);
}
/**
* strLenNoS - returns length of string with no spaces.
* @str: string.
* Return: length of a string.
**/
int strLenNoS(const char *str)
{
int i = 0;
int len = 0;
if (!str)
{
return (len);
}
while (str[i])
{
if (str[i] != ' ')
len++;
else if (str[i] == ' ' && str[i - 1] != ' ' && str[i - 1] != '\0')
len++;
i++;
}
return (len);
}
/**
* strLen - returns length of string.
* @str: string.
* Return: length of a string.
**/
int strLen(const char *str)
{
int len = 0;
if (!str)
return (len);
while (str[len])
len++;
return (len);
}