104 lines
1.9 KiB
C++
104 lines
1.9 KiB
C++
|
#include <stdint.h>
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
#define ALLOCSIZE 10000
|
||
|
|
||
|
static char allocbuf[ALLOCSIZE];
|
||
|
static char *allocp = allocbuf;
|
||
|
|
||
|
char *alloc(int n)
|
||
|
{
|
||
|
if (n % 4 != 0) {
|
||
|
n += 4 - (n % 4);
|
||
|
}
|
||
|
|
||
|
if (allocbuf + ALLOCSIZE - allocp >= n) {
|
||
|
allocp += n;
|
||
|
return allocp - n;
|
||
|
} else
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
#define NULL ((void*) 0)
|
||
|
|
||
|
bool is_delim(char c, const char *delims) {
|
||
|
while (*delims) {
|
||
|
if (c == *delims) {
|
||
|
return true;
|
||
|
}
|
||
|
delims++;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
char *simple_strtok(char *str, const char *delims) {
|
||
|
static char *static_str = (char *) NULL;
|
||
|
|
||
|
if (str == NULL) {
|
||
|
return (char *) NULL;
|
||
|
}
|
||
|
static_str = str;
|
||
|
|
||
|
while (*static_str && is_delim(*static_str, delims)) {
|
||
|
static_str++;
|
||
|
}
|
||
|
|
||
|
if (*static_str == '\0') {
|
||
|
return (char *) NULL;
|
||
|
}
|
||
|
|
||
|
char *token_start = static_str;
|
||
|
|
||
|
while (*static_str && !is_delim(*static_str, delims)) {
|
||
|
static_str++;
|
||
|
}
|
||
|
|
||
|
if (*static_str) {
|
||
|
*static_str = '\0';
|
||
|
static_str++;
|
||
|
}
|
||
|
|
||
|
return token_start;
|
||
|
}
|
||
|
|
||
|
#define MAX_WORDS 100
|
||
|
|
||
|
struct model {
|
||
|
char *ptr;
|
||
|
uint32_t len;
|
||
|
} words[MAX_WORDS];
|
||
|
|
||
|
int word_count = 0;
|
||
|
|
||
|
int strlen(char *s) {
|
||
|
char *p = s;
|
||
|
while (*p != '\0')
|
||
|
p++;
|
||
|
return p - s;
|
||
|
}
|
||
|
|
||
|
int alg(char *ptr) {
|
||
|
const char *delims = " ,.!?:;\n\t";
|
||
|
int pos = 0;
|
||
|
|
||
|
while (char *token = simple_strtok(ptr, delims)) {
|
||
|
words[pos].ptr = token;
|
||
|
words[pos].len = strlen(token);
|
||
|
++pos;
|
||
|
}
|
||
|
|
||
|
return pos;
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
char *str = "Success is often defined as the ability to reach your goals in life, whatever those goals may be. In some ways, a better word for success might be attainment, accomplishment, or progress. It is not necessarily a destination but a journey that helps develop the skills and resources you need to thrive.";
|
||
|
|
||
|
|
||
|
word_count = alg(str);
|
||
|
|
||
|
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|