fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. char str[] = "abc.def.swscan.apple.com";
  7. int init_size = strlen(str);
  8. char delim[] = ".";
  9. char *lastElement[2] = {0};
  10.  
  11. char *ptr = strtok(str, delim);
  12.  
  13. while(ptr != NULL)
  14. {
  15. lastElement[0] = lastElement[1];
  16. lastElement[1] = ptr;
  17.  
  18. printf("'%s'\n", ptr);
  19. ptr = strtok(NULL, delim);
  20. }
  21. if(lastElement[2]) {
  22. printf("%s\n", lastElement[2]);
  23. }
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0.01s 5496KB
stdin
Standard input is empty
stdout
'abc'
'def'
'swscan'
'apple'
'com'