4

Splitting a string with a defined delimiter and then using indexes to call the element required is a pretty easy job in python. Specifically, calling the second last element from a list of variable length(since the string given can have a lot of elements after splitting), is a cakewalk because of the simple syntax.

Example:

str = "swscan.apple.com"
str_list = str.split(".")
print(str_list)
print(str_list[-2])

Output of this would be:

['swscan', 'apple', 'com']
apple

But doing the same thing in C is a pretty tedious job. I have to use strtok function for the same, which is further stored in a pointer value, and is then traversed through, which gives us the elements after splitting.

I am able to achieve this, but the thing that really puts me off is the part where I have to access the second last element of the same. Can somebody help me on how this can be achieved? Here is what I have done so far:

int main()
{
    char str[] = "swscan.apple.com";
    int init_size = strlen(str);
    char delim[] = ".";

    char *ptr = strtok(str, delim);

    while(ptr != NULL)
    {
        printf("'%s'\n", ptr);
        ptr = strtok(NULL, delim);
    }
    return 0;
}

The output of this would be:

'swscan'
'apple'
'com'

But this still does not come in a structured format with which I can access the second last element using the indexes. Any help would be appreciated. Thanks in advance.

Devanshu Misra
  • 773
  • 1
  • 9
  • 28
  • Do you want any arbitrary element or always second last element? – Mohit Jain Oct 24 '18 at 06:11
  • How about an array of pointers that you initialize with your `strtok` loop? By keeping track of the current element you're initializing, then you will know the "size" after the loop, and can then access any arbitrary (and valid) element. – Some programmer dude Oct 24 '18 at 06:12
  • @MohitJain The second last element will be the most suited element. Although a check would be there to see if this is exactly what I want. But if it is not that desired element, then probably the third last element would do. – Devanshu Misra Oct 24 '18 at 06:14
  • @Someprogrammerdude Will it be helpful if I initialize the array pointer with strtok, and then inside the loop where I print the elements I get, I assign the element with the array pointer with an index. Can this be achieved? That way, the total length of my array would be the number of elements, and then getting any arbitrary element would be possible. – Devanshu Misra Oct 24 '18 at 06:16
  • I think the answer to this question might be what you are interested in. [link](https://stackoverflow.com/questions/11198604/c-split-string-into-an-array-of-strings/11198645) – Oukaasha Habib Oct 24 '18 at 06:28

1 Answers1

2

You can store the last two items in an array and print the second last item.

[...]
    char delim[] = ".";
    char *lastElement[2] = {0};  /* 1. To store delimited items */

    char *ptr = strtok(str, delim);

    while(ptr != NULL)
    {
        lastElement[0] = lastElement[1];  /* 2. Update */
        lastElement[1] = ptr;
        printf("'%s'\n", ptr);
        ptr = strtok(NULL, delim);
    }
    if(lastElement[0]) {  /* 3. Does second last item exist */
        printf("%s\n", lastElement[0]);  /* 4. Second last item */
    }
[...]

Check on ideone

This idea can be extended to get any arbitrary element also.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • So, if I create an array of size say [3], the possibility of getting the third last element would be there? Also, is this code specifically to a string that has just 3 elements after splitting or for 'n' number of elements after splitting? – Devanshu Misra Oct 24 '18 at 06:24
  • 1
    It will work for `'n'` number of elements after splitting. – Mohit Jain Oct 24 '18 at 06:25