FlippyPass/project/backend/base.c

40 lines
No EOL
1.1 KiB
C

// Header
#include "base.h"
// Libraries
#include <furi.h>
// Functions
char** split_string(const char* str, char delimiter, int* count) {
// Calculate how many substrings will be created
int i, numSubstrings = 1;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == delimiter) {
numSubstrings++;
}
}
// Allocate memory for the array of strings
char** result = malloc(numSubstrings * sizeof(char*));
*count = numSubstrings;
// Split the string
int start = 0, substringIndex = 0;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == delimiter) {
int length = i - start;
result[substringIndex] = malloc((length + 1) * sizeof(char));
strncpy(result[substringIndex], str + start, length);
result[substringIndex][length] = '\0';
start = i + 1;
substringIndex++;
}
}
// Add the last substring
int length = i - start;
result[substringIndex] = malloc((length + 1) * sizeof(char));
strncpy(result[substringIndex], str + start, length);
result[substringIndex][length] = '\0';
return result;
}