免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4544 | 回复: 1
打印 上一主题 下一主题

[函数] 添加和删除转义符 [复制链接]

larkinboy 该用户已被删除
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-22 17:30 |只看该作者 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
2 [报告]
发表于 2011-03-22 17:54 |只看该作者

  1. #include <string.h>

  2. #define ESCAPE_CHAR   '/'

  3. char *decode_escape_char(char *text)
  4. {
  5.         int i, j;

  6.         j = 0;
  7.         for (i = 0; text[i] != '\0'; i++) {
  8.                 if (text[i] == ESCAPE_CHAR) {
  9.                         text[j++] = text[++i];
  10.                 }else {
  11.                         text[j++] = text[i];
  12.                 }
  13.         }
  14.         text[j] = '\0';
  15.         return text;
  16. }

  17. char *encode_escape_char(char *text, int len)
  18. {
  19.         int i, j;

  20.         j = 0;
  21.         for (i = 0; i < len && text[i] != '\0'; i++) {
  22.                 if (text[i] == ESCAPE_CHAR) {
  23.                         if (i + 1 < len) {
  24.                                 memmove(text + i + 1, text + i, len - i - 1);
  25.                         }
  26.                         text[i] = ESCAPE_CHAR;
  27.                         i++;
  28.                 }
  29.         }
  30.         return text;
  31. }

  32. #include <stdio.h>

  33. int main(void)
  34. {
  35.         char text[] = "abc ///0/r defg";
  36.         puts(decode_escape_char(text));
  37.         puts(encode_escape_char(text, sizeof(text)));
  38.         return 0;
  39. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP