Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char **get_sub_strings(char text_line[], const char sub_start[],
- const char sub_end)
- { //GIVEN "this is <a href="link 1"> test1 test <a href="test2">"::
- int sub_index = 0;
- char *cursor = NULL; //indexed @ 1st occurrence of link
- char *end = NULL; //index 1 after end of link
- char **sub_strings = NULL; //2d array to hold all strings
- int sub_count = count_sub_strings(text_line, sub_start); // 2
- if (sub_count > 0) {
- sub_strings = malloc(sizeof(char*) * (sub_count) ); // create 2d Array
- cursor = strstr(text_line, sub_start); // will cut of "this is "
- // (locates first occurrence of STRING sub_start)
- while (cursor != NULL) { //while ! eo string
- end = strchr(cursor, sub_end) + 1; //will cut off "this is <a href="link">"
- // (locates 1st occurence of CHARACTER sub_end)
- int sub_length = (end - cursor); // 17
- sub_strings[sub_index] = (char *) malloc(sub_length + 1); //sub_strings[x] will hold pointer to string
- for (int i = 0; i < sub_length; ++i) { // 17 loops
- 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
- if (sub_strings[sub_index][i] == sub_end) { // if index char is ">"
- sub_strings[sub_index][i+1] = '\0'; // append string-terminating null at end to indicate end of string
- }
- }
- cursor = strstr(end + 1, sub_start); // returns pointer to next occurrence of <a
- sub_index++;
- }
- }
- return sub_strings;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement