#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>/* return the portion of the string starting after the first * non-word character before the cursor up to the cursor */char *get_word_to_cursor(char *s, int cursor){ char *word = NULL; int i; if((word = malloc(1 + cursor)) == NULL) exit(EXIT_FAILURE); word[0] = '\0';// memset(word, 0, cursor + 1); if(cursor > 0) { /* loop backwards through s from the cursor until we hit a * non-word character */ for(i = cursor; i > 0 && (isalnum(s[i - 1]) || s[i - 1] == '_'); i--); /* i is now the index of the character after the first * non-word character before the cursor */ strncpy(word, s + i, cursor - i); } return word;}int main(void){ char string[] = "a test string"; char *segment = NULL; int cursor = 10; segment = get_word_to_cursor(string, cursor); printf("<%s>",segment); free(segment); return 0;}