-2

I have a variable that holds these sets of characters:

char recvBuff[1024] = "OK lastname,firstname 127.0.0.1";

I'm trying to parse the 127.0.0.1 IP address from that entire string and assign it to another variable. How can I parse this IP address from the string? (I don't need 127, 0, 0, and 1 separate. I need "127.0.0.1" as a whole string.

1 Answers1

1

You can use strtok() or strtok_r() function to separate the string.

      char *strtok(char *str, const char *delim);

      char *strtok_r(char *str, const char *delim, char **saveptr);

Before separating the string You can take the backup of the string because it will affect the original string.

sharon
  • 734
  • 6
  • 15
  • perfect, thank you. i had to create a token `char *token` then type the same line three times to get to the IP address `token = strtok(recvBuff, " ");` (at this point the token is at the OK characters, then `token = strtok(NULL, " ");` (the token is now the lastname,firstname) and finally do the same thing one more time to make the token equal to the IP address. Is there a way to simplify this? – user2921302 Apr 08 '15 at 07:23
  • Are you using `"OK lastname,firstname 127.0.0.1"` string only or you will have some other strings and at last `address`? – sharon Apr 08 '15 at 07:28