免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123456789
最近访问板块 发新帖
楼主: kewenliang
打印 上一主题 下一主题

一道百度笔试题 [复制链接]

论坛徽章:
2
2016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之广夏
日期:2016-12-07 08:32:11
81 [报告]
发表于 2014-08-10 14:11 |只看该作者
直接上汇编!

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
82 [报告]
发表于 2014-08-10 14:27 |只看该作者
显然前面的代码乎略了汉字中的英文字母的转换。

论坛徽章:
0
83 [报告]
发表于 2014-08-11 03:50 |只看该作者
说实话,我用c比较多,瞬间有点没头绪。

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
84 [报告]
发表于 2014-08-11 11:49 |只看该作者
给个通用的函数,并附例子。

  1. /* nstrcovt.c */

  2. #include <string.h>

  3. int nstr_get_nchar(const char *s, int off, int lmt)
  4. {
  5.         if (off < lmt) {
  6.                 if (s[off] < 0 && off + 1 < lmt) {
  7.                         return 2;
  8.                 }
  9.                 return 1;
  10.         }
  11.         return 0;
  12. }

  13. int nchar_match(const char *c, int c_len, const char *list, int list_len)
  14. {
  15.         int i;
  16.         int len;

  17.         for (i = 0; i + c_len <= list_len; i += len) {
  18.                 len = nstr_get_nchar(list, i, list_len);
  19.                 if (len == c_len && strncmp(c, list + i, len) == 0) {
  20.                         return i;
  21.                 }
  22.         }
  23.         return -1;
  24. }

  25. int nstr_convert(char *s, int off, int lmt, const char *src, const char *tar)
  26. {
  27.         int c_len;
  28.         int i;
  29.         int src_len;

  30.         src_len = strlen(src);
  31.         for (; off < lmt; off += c_len) {
  32.                 c_len = nstr_get_nchar(s, off, lmt);
  33.                 i = nchar_match(s + off, c_len, src, src_len);
  34.                 if (i != -1) {
  35.                         strncpy(s + off, tar + i, c_len);
  36.                 }
  37.         }
  38.         return 0;
  39. }

  40. /* test */

  41. #if 1

  42. #include <stdio.h>

  43. int main(void)
  44. {
  45.         char str[] = "abcdefghijklmnopqrstuvwxyzавс";
  46.         char rep[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZАВС";
  47.         char buf[] = "I LIKE АВС";

  48.         nstr_convert(buf, 0, strlen(buf), rep, str);
  49.         puts(buf);
  50.         return 0;
  51. }

  52. #endif
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP