question about strtok_r built-in function in C
If a string has many words in it then, strtok_r will not work correctly.
Note: A word within a string is separated by a blank space.
s_1 output is incorrect. s_1 should be 10.4.0.1
If a string has only one word in it, then strtok_r will work correctly.
s_2 output is correct. s_2 output is 10.4.0.1
Is there a function that will do what strtok_r except that it will
search for a long string that contains many words ? Using a built-in
function to search for a word or a character pattern within a very long
string saves time because I don 't have to use a loop with an if-then-else
statement.
==================================
root:/home# ./string_1
using strtok_r function. s_1 = bbb
using strtok_r function. s_2 = 10.4.0.1
==================================
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main ()
{
char ip_address_string_1[30] = "aaa bbb ddd addr:10.4.0.1";
char ip_address_string_2[30] = "addr:10.4.0.1";
char *s_1;
char *s_2;
char *save_pointer;
char *ip_address;
s_1 = strtok_r(ip_address_string_1, "addr:", &save_pointer);
printf ("using strtok_r function. s_1 = %s\n", s_1);
s_2 = strtok_r(ip_address_string_2, "addr:", &save_pointer);
printf ("using strtok_r function. s_2 = %s\n", s_2);
}