Advertisement
sauerCHAOS

Name completion text splitter

Aug 14th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main(void)
  7. {
  8.     static char lbuf[1024];
  9.     static char rbuf[1024];
  10.     static char cbuf[1024];
  11.    
  12.     //const char *str = "testing between two spaces here --->    <--- ok";
  13.     const char *str = "hello xyz world";
  14.     size_t cursor_pos = 9;
  15.    
  16.     const size_t len = strlen(str);
  17.     size_t split_pos = cursor_pos ? cursor_pos - 1 : 0;
  18.    
  19.     while (split_pos > 0 && !isspace(str[split_pos])) {
  20.         --split_pos;
  21.     }
  22.    
  23.     memcpy(lbuf, str, split_pos + 1);
  24.     memcpy(rbuf, str + cursor_pos, len - cursor_pos);
  25.     memcpy(cbuf, str + split_pos + 1, cursor_pos - split_pos);
  26.  
  27.     printf("%s\n", lbuf);
  28.     printf("%s\n", rbuf);
  29.     printf("%s\n", cbuf);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement