练习题2
写一个函数,删除给定范围的字符。如:char str[] = "123456789"; str_delete(str, 3, 6); printf("%s\n", str);
输出:123789
补充:
对于范围不合法的,不对字符串做处理。
悬赏分:15
参考答案:
程序代码:
#include <stdio.h> #include <string.h> char * del(char * str, unsigned begin, unsigned end) { unsigned i = end, j = strlen(str), range = end - begin; if(begin > j || end > j || end < begin) return str; while(i <= j) str[i - range] = str[i++]; return str; } int main(void) { char str[] = "123456789"; del(str, 3, 6); printf("%s\n", str); return 0; } /* Output: 123789 Process returned 0 (0x0) execution time : 0.266 s Press any key to continue. */
[ 本帖最后由 lz1091914999 于 2011-6-25 19:29 编辑 ]