Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. char **get_sub_strings(char text_line[], const char sub_start[],
  2. const char sub_end)
  3. { //GIVEN "this is <a href="link 1"> test1 test <a href="test2">"::
  4. int sub_index = 0;
  5. char *cursor = NULL; //indexed @ 1st occurrence of link
  6. char *end = NULL; //index 1 after end of link
  7. char **sub_strings = NULL; //2d array to hold all strings
  8. int sub_count = count_sub_strings(text_line, sub_start); // 2
  9.  
  10. if (sub_count > 0) {
  11. sub_strings = malloc(sizeof(char*) * (sub_count) ); // create 2d Array
  12. cursor = strstr(text_line, sub_start); // will cut of "this is "
  13. // (locates first occurrence of STRING sub_start)
  14. while (cursor != NULL) { //while ! eo string
  15. end = strchr(cursor, sub_end) + 1; //will cut off "this is <a href="link">"
  16. // (locates 1st occurence of CHARACTER sub_end)
  17. int sub_length = (end - cursor); // 17
  18. sub_strings[sub_index] = (char *) malloc(sub_length + 1); //sub_strings[x] will hold pointer to string
  19. for (int i = 0; i < sub_length; ++i) { // 17 loops
  20. sub_strings[sub_index][i] = cursor[i]; // for each loop, populate the string pointed to by sub_strings[x] with cursor[i] character-by-character
  21. if (sub_strings[sub_index][i] == sub_end) { // if index char is ">"
  22. sub_strings[sub_index][i+1] = '\0'; // append string-terminating null at end to indicate end of string
  23. }
  24. }
  25. cursor = strstr(end + 1, sub_start); // returns pointer to next occurrence of <a
  26. sub_index++;
  27. }
  28. }
  29.  
  30. return sub_strings;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement