char* mid(char* des, int size, char* src, int first, int count) { int len_src,real_ceil_length; int k; char *i,*j; if(src == 0 || des == 0) { return 0; } i = src; len_src = 0; while(*i != '\0') {i++; len_src++; } i = src; if(first > len_src || first <= 0) { return des; } if((count > len_src - first + 1)||(count == -1)) { real_ceil_length = len_src - first + 1; } else { real_ceil_length = count; } if(real_ceil_length + 1 > size) { return des; } i = src + first - 1; j = des; for(k = 1;k <= real_ceil_length;k++) {*j = *i; i++; j++; } *j = '\0'; return des; } 的用例示范
时间: 2024-02-14 15:36:09 浏览: 65
PHP取整函数:ceil,floor,round,intval的区别详细解析
以下是该函数的用例示范:
```c++
#include <iostream>
#include <cstring>
using namespace std;
char* mid(char* des, int size, char* src, int first, int count);
int main() {
char src[] = "Hello, world!";
char des[20] = "";
mid(des, 20, src, 2, 5);
cout << des << endl; // 输出 "ello,"
mid(des, 20, src, 7, -1);
cout << des << endl; // 输出 "world!"
mid(des, 10, src, 14, 3);
cout << des << endl; // 输出 ""
return 0;
}
char* mid(char* des, int size, char* src, int first, int count) {
int len_src, real_ceil_length;
int k;
char *i,*j;
if(src == 0 || des == 0) {
return 0;
}
i = src;
len_src = 0;
while(*i != '\0') {
i++;
len_src++;
}
i = src;
if(first > len_src || first <= 0) {
return des;
}
if((count > len_src - first + 1)||(count == -1)) {
real_ceil_length = len_src - first + 1;
} else {
real_ceil_length = count;
}
if(real_ceil_length + 1 > size) {
return des;
}
i = src + first - 1;
j = des;
for(k = 1;k <= real_ceil_length;k++) {
*j = *i;
i++;
j++;
}
*j = '\0';
return des;
}
```
该示范程序中,使用该函数从源字符串中取出中间部分,并将结果保存在目标字符串中。其中,函数的参数包括目标字符串指针、目标字符串缓冲区长度、源字符串指针、开始位置和取得的字符数。函数的返回值是结果字符串指针。
示范程序中,分别取出了源字符串中的 "ello," 和 "world!" 两个部分,并将结果保存在目标字符串中。同时,还测试了当目标字符串缓冲区长度不足时和开始位置越界时的情况。
阅读全文