-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrings-2.c
126 lines (109 loc) · 2.08 KB
/
strings-2.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
#include "shell.h"
/**
* clean_comments - remove the comments from a buffer.
* @buffer: buffer to clean.
* Return: a pointer to the new (without comments) buffer.
*/
char *clean_comments(char *buffer)
{
char *clean_buffer;
int i = 0;
clean_buffer = malloc(sizeof(char) * (str_len(buffer) + 1));
if (!clean_buffer)
{
perror("MALLOC");
return (NULL);
}
while (buffer[i])
{
if (buffer[i] == '#')
break;
clean_buffer[i] = buffer[i];
i++;
}
clean_buffer[i] = '\0';
return (clean_buffer);
}
/**
* not_empty - checks if buffer contains only spaces
* @input_buffer: command line to be executed
* Return: 0 if input_buffer contains only spaces, -1 if contains another char.
*/
int not_empty(char *input_buffer)
{
int i = 0;
int boolean = 0;
while (input_buffer[i])
{
if (input_buffer[i] != ' ')
break;
i++;
}
if (input_buffer[i] && input_buffer[i] != '\n')
boolean = -1;
return (boolean);
}
/**
* str_twins - compares two strings.
* @s1: string 1.
* @s2: string 2.
* Return: 0 if s1 == s2, -1 if s1 != s2.
*/
int str_twins(char *s1, char *s2)
{
int i = 0;
while (s1[i] && s2[i])
{
if (s1[i] != s2[i])
return (-1);
i++;
}
if (s1[i] != s2[i])
return (-1);
return (0);
}
/**
* str_count - counts the number of times the char c is used in the buffer.
* @buffer: buffer to check.
* @c: char.
* Return: number of repetitions +1.
*/
int str_count(char *buffer, char c)
{
int i = 0;
int counter = 0;
while (buffer[i])
{
if (buffer[i] == c)
counter++;
i++;
}
return (counter + 1);
}
/**
* str_tr - swap the character old_char by the character new_char
* all the times it appears in the buffer.
* @buffer: input buffer.
* @old_char: char to be changed.
* @new_char: char that replaces the old.
* Return: the modified buffer.
*/
char *str_tr(char *buffer, char old_char, char new_char)
{
int i = 0;
char *new_buffer;
new_buffer = str_dup(buffer);
while (buffer[i])
{
new_buffer[i] = buffer[i];
i++;
}
i = 0;
while (new_buffer[i])
{
if (new_buffer[i] == old_char)
new_buffer[i] = new_char;
i++;
}
return (new_buffer);
}