#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
static size_t words_st(const char* s, const char* cs, char* d);
char* find_words(const char* s, const char* cs);
int main(void){
char s[] = "ADA, lisp, ocean, (match) sea, spy, lake, ALGOL";
char* p = find_words(s, "Aa");
if(p != NULL){
puts(p);
//...
free(p);
}
return 0;
}
char* find_words(const char* s, const char* cs){
char* p;
size_t n = words_st(s, cs, NULL);
if(! n)
return NULL;
p = (char*)malloc((n + 1) * sizeof(char));
if(p != NULL)
words_st(s, cs, p);
return p;
}
static size_t words_st(const char* s, const char* cs, char* d){
size_t n = 0, k = 0, i = 0;
do {
if(isalpha(*s)){
++k;
if(! i){
if(strchr(cs, *s) != NULL)
i = 1;
}
} else if(k > 0){
if(i){
if(d == NULL)
n += k + 1;
else {
strncpy(d, s - k, k);
d += k;
*d++ = ' ';
}
}
k = i = 0;
}
} while(*s++ != '\0');
if(d != NULL)
*d = '\0';
return n;
}