Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1435193
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2013-12-25 19:31:13

题目:字符串常用操作,包括:字符串长度、比较、拷贝、连接、子串查找、子串截取(持续增加....)
分析:主要考察指针操作,小心边界溢出、越界等问题
代码:

点击(此处)折叠或打开

  1. /******************************************************************************

  2.   Copyright (C), 2001-2011, HW Tech. Co., Ltd.

  3.  ******************************************************************************
  4.   File Name :
  5.   Version :
  6.   Author :
  7.   Created : 2012/03/12
  8.   Last Modified :
  9.   Description :
  10.   Function List :
  11.               
  12.   History :
  13.   1.Date : 2012/03/12
  14.     Author :
  15.     Modification: Created file

  16. ******************************************************************************/
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>

  20. unsigned int strlenth(char *s) /* 获取字符串长度 */
  21. {
  22.     unsigned int lenth = 0;

  23.     if (NULL == s)
  24.         return lenth;

  25.     // 代码在这里实现
  26.     char* tmp = s;
  27.     while (*tmp != '\0')
  28.     {
  29.         lenth ++;
  30.         tmp++;
  31.     }

  32.     return lenth;
  33. }

  34. void strcopy(char **target, char *source) /* 字符串拷贝 */
  35. {
  36.     /* 代码在这里实现 */
  37.     if ( (NULL == source) || (NULL == target))
  38.         return;

  39.     char* dest;
  40.     char* src = source;
  41.     int len = strlenth(source);
  42.     dest = (char*)malloc(len+1);
  43.     if (NULL == dest)
  44.         return;
  45.     memset( dest,'\0',(len+1) );
  46.      *target = dest;

  47.     while (*src != '\0')
  48.     {
  49.         *dest = *src;
  50.         src++;
  51.         dest++;
  52.     }

  53.     return;
  54. }

  55. int strcompare(char *s, char *t) /* 字符串比较,s>t,则返回1;s=t,则返回0;s<t,则返回-1 */
  56. {
  57.     /* 代码在这里实现 */
  58.     if ( (NULL == s) || (NULL == t))
  59.         return 0;

  60.     char* str1 = s;
  61.     char* str2 = t;

  62.     while ( (*str1 != '\0') && (*str2 != '\0'))
  63.     {
  64.         if (*str1 > *str2)
  65.             return 1;
  66.         else if (*str1 < *str2)
  67.             return -1;
  68.         else
  69.         {
  70.             str1++;
  71.             str2++;
  72.         }
  73.     }
  74.     if ( (*str1 == '\0') && (*str2 == '\0') )
  75.         return 0;
  76.     else if ( (*str1 == '\0') && (*str2 != '\0' ) )
  77.         return -1;
  78.     else if ( (*str1 != '\0') && (*str2 == '\0') )
  79.         return 1;
  80.     
  81.     return 0;
  82. }

  83. void strcombine(char **x, char *s, char *t) /* 字符串连接,将字符串t接到s后面,x为连接后的新串 */
  84. {
  85.     /* 代码在这里实现 */
  86.     if ( (NULL == s) || (NULL == t) || (NULL == x))
  87.         return ;

  88.     int len_s = strlenth(s);
  89.     int len_t = strlenth(t);
  90.     char* dest;
  91.     dest = (char*)malloc(len_s + len_t + 1);
  92.     if (NULL == dest)
  93.         return ;
  94.     memset(dest,'\0',(len_s + len_t));

  95.     *x = dest;

  96.     char *tmp = s;
  97.     while (*tmp != '\0')
  98.     {
  99.         *dest = *tmp;
  100.         tmp++;
  101.         dest++;
  102.     }
  103.     tmp = t;
  104.     while (*tmp != '\0')
  105.     {
  106.         *dest = *tmp;
  107.         tmp++;
  108.         dest++;
  109.     }
  110.     *dest = '\0';
  111. }

  112. void strcatch(char *s, unsigned int index, unsigned int lenth, char **t) /* 字符串截取,从第index个字符开始,截取lenth长度的字符串,并输出到字符串t */
  113. {
  114.     /* 代码在这里实现 */
  115.     unsigned int len = strlenth(s);
  116.     if ( (NULL == s) || (len<=0) || (index<0) || (index>=len) || (NULL == t) || (lenth<=0) || (index+lenth>len) )
  117.         return ;

  118.     char *dest;
  119.     dest = (char*)malloc(index+1);
  120.     if (NULL == dest)
  121.         return ;

  122.     *t = dest;
  123.     unsigned int count = 0;
  124.     char *tmp = s;
  125.     while ( (*tmp != '\0') && (count < index) )
  126.     {
  127.         count++;
  128.         tmp++;
  129.     }
  130.     while ( (*tmp != '\0') && (lenth > 0) )
  131.     {
  132.         *dest = *tmp;
  133.         dest++;
  134.         tmp++;
  135.         lenth--;
  136.     }
  137.     *dest = '\0';
  138. }


  139. bool strsubstr(char *s, char *sub) /* 字符串子串查找,如果子串sub在s中存在,则返回1,否则返回0 */
  140. {
  141.     bool result = 0;

  142.     /* 代码在这里实现 */
  143.     if ( (NULL == s) || (NULL == sub) || (strlenth(s)<strlenth(sub)) )
  144.         return 0;
  145.     char *tmp = s;
  146.     char *tmp_str;

  147.     while (*tmp != '\0')
  148.     {
  149.         tmp_str = sub;
  150.         while ( (*tmp != '\0') && (*tmp_str != '\0'))
  151.         {
  152.             if (*tmp == *tmp_str)
  153.             {
  154.                 tmp++;
  155.                 tmp_str++;
  156.             }
  157.             else
  158.             {
  159.                 break;
  160.             }
  161.         }
  162.         if ( ( (*tmp == '\0') && (*tmp_str == '\0') ) || (*tmp!='\0') && (*tmp_str == '\0'))
  163.         {
  164.             result =1;
  165.             return result;
  166.         }
  167.         else
  168.         {
  169.             tmp++;
  170.         }
  171.     }
  172.     return result;
  173. }

阅读(619) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~