You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.3 KiB
85 lines
1.3 KiB
#include <stdlib.h>
|
|
|
|
int char_is_separator(char c, char *charset)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (charset[i] != '\0')
|
|
{
|
|
if (c == charset[i])
|
|
return (1);
|
|
i++;
|
|
}
|
|
if (c == '\0')
|
|
return (1);
|
|
return (0);
|
|
}
|
|
|
|
int count_words(char *str, char *charset)
|
|
{
|
|
int i;
|
|
int words;
|
|
|
|
words = 0;
|
|
i = 0;
|
|
while (str[i] != '\0')
|
|
{
|
|
if (char_is_separator(str[i + 1], charset) == 1
|
|
&& char_is_separator(str[i], charset) == 0)
|
|
words++;
|
|
i++;
|
|
}
|
|
return (words);
|
|
}
|
|
|
|
void write_word(char *dest, char *from, char *charset)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (char_is_separator(from[i], charset) == 0)
|
|
{
|
|
dest[i] = from[i];
|
|
i++;
|
|
}
|
|
dest[i] = '\0';
|
|
}
|
|
|
|
void write_split(char **split, char *str, char *charset)
|
|
{
|
|
int i;
|
|
int j;
|
|
int word;
|
|
|
|
word = 0;
|
|
i = 0;
|
|
while (str[i] != '\0')
|
|
{
|
|
if (char_is_separator(str[i], charset) == 1)
|
|
i++;
|
|
else
|
|
{
|
|
j = 0;
|
|
while (char_is_separator(str[i + j], charset) == 0)
|
|
j++;
|
|
split[word] = (char*)malloc(sizeof(char) * (j + 1));
|
|
write_word(split[word], str + i, charset);
|
|
i += j;
|
|
word++;
|
|
}
|
|
}
|
|
}
|
|
|
|
char **my_split(char *str, char *charset)
|
|
{
|
|
char **split;
|
|
int words;
|
|
|
|
words = count_words(str, charset);
|
|
split = (char**)malloc(sizeof(char*) * (words + 1));
|
|
split[words] = 0;
|
|
write_split(split, str, charset);
|
|
return (split);
|
|
}
|