-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrfunc.c
More file actions
84 lines (73 loc) · 1.91 KB
/
strfunc.c
File metadata and controls
84 lines (73 loc) · 1.91 KB
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
#include "hdr.h"
char *concat_strings(char *first, char *second, char sep)
/* Combines strings into one new string with separator */
{
int i;
int len_one;
int len_two;
int sep_exists;
char *str;
len_one = str_len(first);
len_two = str_len(second);
sep_exists = 0;
if (sep)
sep_exists = 1;
str = malloc(sizeof(char) * (len_one + len_two + 2));
if (!str) {
return NULL;
}
for (i = 0; i < len_one; i++) {
str[i] = first[i];
}
if (sep_exists)
str[i] = sep;
for (i = 0; i < len_two; i++) {
str[len_one + sep_exists + i] = second[i];
}
str[len_one + sep_exists + i] = '\0';
return str;
}
int str_compare(char *input, char *test)
/* Compares all chars in strings */
{
int i;
for (i = 0; input[i] != '\0'; i++) {
if (input[i] != test[i])
return 0;
}
if (test[i] != '\0')
return 0;
return 1;
}
int str_len(char *str)
/* Returns number of chars in string */
{
int i;
for (i = 0; str[i] != '\0'; i++);
return i;
}
int strn_compare(char *input, char *test, int n)
/* Compares n characters of strings */
{
int i;
for (i = 0; input[i] != '\0' && i < n; i++) {
if (input[i] != test[i])
return 0;
}
if (input[i] == '\0' && test[i] != '\0' && i != n)
return 0;
return 1;
}
char *trim_left(char *str, int n)
/* Returns newly allocated string */
{
int i;
int len;
char *new;
len = str_len(str);
new = malloc(sizeof(char) * (len + 1 - n));
new[len - n] = '\0';
for (i = 0; str[n + i] != '\0'; i++)
new[i] = str[n + i];
return new;
}