华为C语言字符串试题
发布网友
发布时间:2022-05-24 12:17
我来回答
共2个回答
热心网友
时间:2023-10-13 00:30
随便做了下
#include <stdio.h>
#include <stdlib.h>
void take_num(const char *strIn, int *n, unsigned int *outArray) {
char *s = strIn;
int v = 0, len = 0;
if (strIn == NULL || n == NULL || outArray == NULL)
return;
*n = 0;
while (*s != '\0') {
if (*s >= '0' && *s <= '9') {
v = v * 10 + *s - '0';
len++;
}
else {
if (len > 0) {
outArray[*n] = v;
(*n)++;
len = 0;
v = 0;
}
}
s++;
}
if (len > 0) {
outArray[*n] = v;
(*n)++;
}
}
void max_prefix_match(const char *ip_addr, const char *net_addr_array[],
int *n) {
unsigned int num, ip[4], net[8];
unsigned char mask[8] = {
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};
int i, j, index, match, match_length, max_length = 0;
if (ip_addr == NULL || net_addr_array == NULL || n == NULL)
return;
take_num(ip_addr, &num, ip);
*n = -1;
index = 0;
while (*net_addr_array[0]) {
take_num(*net_addr_array, &num, net);
for (match = 1, i = 0; i < 4 && match == 1; i++) {
if ((ip[i] & net[i + 4]) != net[i]) {
match = 0;
}
}
if (match == 1) {
for (match = 1, match_length = 0, i = 0; i < 4 && match == 1; i++) {
for (j = 0; j < 8 && match == 1; j++, match_length++) {
if ((ip[i] & mask[j]) != (net[i] & mask[j])) {
match = 0;
}
}
}
if (match_length > max_length) {
max_length = match_length;
*n = index;
}
}
index++;
net_addr_array++;
}
}
void test1() {
char strIn[] = "ab00cd+123fght456-25 3.005fgh";
int n, outArray[100];
int i;
take_num(strIn, &n, outArray);
printf("%s\n%d->", strIn, n);
for (i = 0; i < n; i++) {
printf(" %d", outArray[i]);
}
if (n == 0)
printf("no valid integer");
printf("\n");
}
void test2() {
char ip_addr[] = "192.168.1.100";
char *net_addr_array[] = {
"192.168.1.128/255.255.255.192",
"192.168.1.0/255.255.255.0",
"192.168.1.64/255.255.255.192",
"0.0.0.0/0.0.0.0", ""
};
int n;
max_prefix_match(ip_addr, net_addr_array, &n);
printf("n=%d", n);
printf("\n");
}
int main() {
test1();
test2();
system("pause");
return 0;
}
热心网友
时间:2023-10-13 00:31
1、在c语言中存储一个字符串,一般有两种方法,一种是字符指针,一种是使用字符数组。2、例如:
const char *str = "hello"; //使用字符串指针
const char str[] = "hello"; //使用字符数组
3、如果保存的字符串,需要修改。一般使用字符数组。
例如:
char path[256] = "c:\\windows\\";
strcat(path, "system32");