所以我正在做this代码战kata,并且在每个测试中都成功,但是最终结果失败了,因为我的代码存在分段错误,而且我认为我对该语言的了解不足以找到它!有人可以暂停吗?
int is_valid_ip(const char *addr)
{
char set[] = "1234567890";
int current;
int octet_counter = 0;
char *octet = 0;
octet = strtok(addr, ".");
while (octet)
{
if (strspn(octet, set) != strlen(octet)) return 0; // checks for spaces
if (strlen(octet) > 1 && (octet[0]) == '0') return 0; // checks for preceding zeros
sscanf(octet, "%d", ¤t);
if (current < 0 || current > 255) return 0; // checks for range
octet = strtok(0, ".");
++octet_counter;
}
if (octet_counter == 4) return 1; // checks for number of octets
return 0;
};
我的代码比较干净,但是在设法解决这个问题之后经过很多麻烦之后,事情就变成了……
答案 0 :(得分:4)
由于strtok()
修改了要标记的字符串,并且addr
被定义为const char *
(我认为这是必要条件),因此您可以复制输入字符串{{1} }:
*addr
随后在char ip[16]; // enought to hold nnn.nnn.nnn.nnn
if(strlen(addr)>15) return 0;
strcpy(ip, addr);
而不是ip
上操作
或者...避免使用addr
并解析/扫描字符串而不修改它。
答案 1 :(得分:0)
这是一个使用sscanf
的愚蠢解决方案,只是为了证明这是可能的。 (下面有一些更好的解决方案。)
#include <stdio.h>
/* True if its argument represents a decimal number between
* 0 and 255 without leading zeros. Assumes the characters are
* all digits and that there are at most three of them.
*/
static int good(const char* octet) {
switch (octet[0]) {
case '0': return octet[1] == 0;
case '1': return 1;
case '2': return octet[1] == 0 || octet[2] == 0
|| octet[1] < '5'
|| octet[1] == '5' && octet[2] < '6';
default: return octet[1] == 0 || octet[2] == 0;
}
}
struct OctetSep { char oct[4], sep[2] };
int is_valid_ip(const char *addr) {
struct OctetSep bits[4];
/* The last %c conversion is expected to fail. */
return sscanf(addr, "%3[0-9]%1[.]%3[0-9]%1[.]%3[0-9]%1[.]%3[0-9]%1c",
bits[0].oct, bits[0].sep,
bits[1].oct, bits[1].sep,
bits[2].oct, bits[2].sep,
bits[3].oct, bits[3].sep) == 7
&& good(bits[0].oct) && good(bits[1].oct)
&& good(bits[2].oct) && good(bits[3].oct);
}
当然,大部分工作是由good
完成的,将其变得更加通用将很容易:
#include <stddef.h>
/* This idiocy is avoid problems with signed characters */
static int my_isdigit(char c) { return c >= '0' && c <= '9'; }
/* Returns the address of the first unmatched character after a match
* of an integer with no leading zeros and maximum value 255.
* This address may be in the middle of the integer, if it is too big.
* If the string doesn't start with a digit, returns NULL.
*/
static const char* good(const char* octet) {
switch (octet[0]) {
case '0': return octet + 1;
case '1': return !my_isdigit(octet[1]) ? octet + 1
: !my_isdigit(octet[2]) ? octet + 2
: octet + 3;
case '2': return !my_isdigit(octet[1]) ? octet + 1
: !my_isdigit(octet[2]) ? octet + 2
: octet[1] < '5' || octet[1] == '5' && octet[2] < '6'
? octet + 3
: octet + 2;
case '3': case '4': case '5': case '6': case '7': case '8':
case '9': return !my_isdigit(octet[1]) : octet + 1 ? octet + 2;
default: return NULL;
}
}
int is_valid_ip(const char *addr) {
for (const char* i = "..."; (addr = good(addr)); ++addr, ++i) {
if (*addr != *i) break;
if (!*i) return 1;
}
return 0;
}
尽管您仍然需要大量手动检查,但最简单的代码可能来自使用strtol
。 (此解决方案可扩展到有效整数范围大于256的应用程序。)
#include <stdlib.h>
/* This idiocy is avoid problems with signed characters */
static int my_isdigit(char c) { return c >= '0' && c <= '9'; }
/* Returns the address of the first character following the integer
* starting precisely at the supplied address, provided the integer
* has no leading zeros and a maximum value of 255. Otherwise,
* returns NULL.
*/
static const char* good(const char* octet) {
if (*octet == '0') return octet + 1;
/* This check is necessary because strtol also accepts whitespace and - */
if (!my_isdigit(*octet)) return NULL;
char *endptr;
long value = strtol(octet, &endptr, 10);
/* We already handled 0, so value==0 must be an error return */
if (value <= 0 || value > 255) return NULL;
return endptr;
}
int is_valid_ip(const char *addr) {
for (const char* i = "..."; (addr = good(addr)); ++addr, ++i) {
if (*addr != *i) break;
if (!*i) return 1;
}
return 0;
}