File: words.c

package info (click to toggle)
libdbi-drivers 0.9.0-13
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,160 kB
  • sloc: ansic: 19,030; sh: 10,963; xml: 2,759; makefile: 584
file content (23 lines) | stat: -rw-r--r-- 576 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string.h>

int split_words(char *sentence) {
    int i, count = 1, length = strlen(sentence);
    for (i = 0; i < length; i++) {
        if (sentence[i] == ' ') {
            sentence[i] = '\0';
            count++;
        }
    }
    return count;
}

void words(const char *sentence, void (*walker)(const char *, void *), void *memo) {
    char *words = strdup(sentence);
    int word_count = split_words(words);
    char *word = words;
    while (word_count-- > 0) {
        (*walker)(word, memo);
        word = word + strlen(word) + 1;
    }
    free(words);
}