This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nova_string.c
112 lines (106 loc) · 3.24 KB
/
nova_string.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
#include"nova_string.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_CARACTERES (256)
#define PASSOUAQUI printf("%s:%d\n", __FILE__, __LINE__);
int substring (char str[], char dest[], char str1[], char str2[]) {
// DIVIDE UMA STRING COM BASE EM OUTROS DOIS DELIMITADORES
// Exemplo: substring("um dois tres quatro", string_destino, "um ", " tres") => retorna "dois"
int i = 0;
int ind1 = 0, ind2 = 0;
while (str[i] != 0) {
if (str[i] == str1[0] && ind1 == 0) {
int e_igual = 1;
for (unsigned int j = 0; j < strlen(str1); j++) {
if (str[i] != str1[j]) {
e_igual = 0;
break;
}
i++;
}
if (e_igual) {
ind1 = i;
}
}
if (strcmp(str2, "\0") == 0) {
ind2 = strlen(str) - 1;
} else if (str[i] == str2[0] && ind2 == 0) {
int tempind = i;
int e_igual = 1;
for (unsigned int j = 0; j < strlen(str2); j++) {
if (str[i] != str2[j]) {
e_igual = 0;
break;
}
i++;
}
if (e_igual) {
ind2 = tempind;
}
}
i++;
}
if (ind1 != 0 && ind2 != 0) {
int dest_counter = 0;
for (int j = ind1; j < ind2; j++) {
if (j == ind2 - 1 && str[j] == ' ') {
break;
}
dest[dest_counter] = str[j];
dest_counter++;
}
dest[dest_counter] = 0;
return 1;
} else {
return 0;
}
}
int contaOcorrenciasString (char *str, char *pedaco) {
// RETORNA O NUMERO DE OCORRENCIAS DE UMA SUBTRING NA STRING
int ocorrencias = 1;
int l1 = strlen(str);
int l2 = strlen(pedaco);
for (int i = 0; i < l1 - l2 + 1; i++) {
if (strstr(str + i, pedaco) == str + i) {
ocorrencias++;
i = i + l2 - 1;
}
}
return ocorrencias;
}
char **separaString (char *str_inp, char *espacador) {
// RETORNA UM ARRAY DE STRINGS, COM BASE NO ESPACADOR DO PARAMETRO
// Exemplo: separaString("heitor e torrent", " e ") => retorna ["heitor", "torrent"]
int num_strings = contaOcorrenciasString(str_inp, espacador);
char string_formatada[strlen(str_inp)];
int g = 0, h = 0;
while(str_inp[g] != '\0'){
int achou_palavra = 1;
for (unsigned int count = 0; count < strlen(espacador); count++) {
achou_palavra = achou_palavra && str_inp[g + count] == espacador[count];
}
if (achou_palavra){
string_formatada[h] = '&';
g += strlen(espacador);
} else {
string_formatada[h] = str_inp[g];
}
g++;
h++;
}
string_formatada[h] = '\0';
char **arr_dest = malloc(sizeof(char *) * num_strings);
for (int i = 0; i < num_strings; i++) {
arr_dest[i] = malloc(sizeof(char) * NUM_CARACTERES);
}
char *pont_str;
pont_str = strtok(string_formatada, "&");
int i = 0;
while (pont_str != NULL){
strcpy(arr_dest[i], pont_str);
i++;
pont_str = strtok(NULL, "&");
}
return arr_dest;
}