What would be a good way to determine if a string contains an IPv4 address? Should I use isdigit()?
What would be a good way to determine if a string contains an IPv4
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I asked a similar question for C++. You should be able to use a slightly modified (for C) version of what I came up with back then.
You’ll need to
#include <arpa/inet.h>to use the inet_pton() function.Update based on comments to the question: If you want to know if a C-style string contains an IP address, then you should combine the two answers given so far. Use a regular expression to find patterns that roughly match an IP address, then use the function above to check the match to see if it’s the real deal.