Learner Level
Implement the validateIpSegmentSimple method that checks whether a text segment is valid for an IP address part.
The input contains a string segment. Your task is to check whether it is a valid IPv4 address segment.
A valid segment must contain only digits, must represent a number from 0 to 255, and must not have leading zeros unless the segment is exactly 0. For example, 255 is valid, 256 is invalid, and 01 is invalid.
Validate the segment using the standard IPv4 rules.
First reject empty strings and strings containing non-digit characters. Then reject segments with more than one character that start with 0, because leading zeros are not allowed.
Finally, convert the segment to a number and check whether it is between 0 and 255.
Pseudocode:
function validateIpSegmentSimple(segment):
if segment is empty:
return false
if segment contains any non-digit character:
return false
if length(segment) > 1 and segment[0] == '0':
return false
value = convert segment to integer
return value >= 0 and value <= 255