C++常用库函数atoi,itoa,strcpy,strcmp的实现

原文链接:谢谢 作者

strcmp、strcpy、strlen的实现

[cpp]  view plain copy
  1. #include <assert.h>  
  2. char *strcpy(char *dst, const char *src)//使用const来约束src,表明src对应的内容不能被修改。  
  3. {  
  4.     assert((dst != NULL) && (src != NULL));//使用断言assert来检验输入参数的有效性  
  5.     char *tmp = dst;  
  6.     while ((*dst++ = *src++) != '\0')  
  7.      /* nothing */;  
  8.     return tmp;//返回dst,以便实现链式表达式      
  9. }  
  10.   
  11.   
  12. int strlen(const char * str)   
  13. {  
  14.      assert( str != NULL );  
  15.      const char *cp =  str;  
  16.      while (*cp++ )  
  17.           ;  
  18.      return (cp - str - 1 );  
  19.  }   
  20.   
  21.   
  22. int strcmp(const char *src, const char *dst)  
  23. {  
  24.      assert((dst != NULL) && (src != NULL));  
  25.      int ret = 0 ;   
  26.      while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)   
  27.      { ++src; ++dst; }   
  28.      if ( ret < 0 )   
  29.          ret = -1 ;   
  30.      else if ( ret > 0 )   
  31.          ret = 1 ;   
  32.      return ret;  
  33. }  
atoi,itoa,strcpy,strcmp的实现

[c-sharp]  view plain copy
  1. 1.//整数转换成字符串itoa函数的实现  
  2. #include "stdafx.h"  
  3. #include <iostream>  
  4. using namespace std;  
  5. void itoaTest(int num,char str[] )  
  6. {  
  7.        int sign = num,i = 0,j = 0;  
  8.        char temp[11];  
  9.        if(sign<0)//判断是否是一个负数  
  10.        {  
  11.               num = -num;  
  12.        };  
  13.        do  
  14.        {  
  15.               temp[i] = num%10+'0';          
  16.               num/=10;  
  17.               i++;  
  18.        }while(num>0);  
  19.        if(sign<0)  
  20.        {  
  21.               temp[i++] = '-';  
  22.        }  
  23.        temp[i] = '/0';  
  24.        i--;  
  25.        while(i>=0)  
  26.        {  
  27.               str[j] = temp[i];  
  28.               j++;  
  29.               i--;  
  30.        }  
  31.        str[j] = '/0';  
  32. }  
  33. 2. //字符串转换成整数atoi函数的实现  
  34. int atoiTest(char s[])  
  35. {  
  36.        int i = 0,sum = 0,sign;    //输入的数前面可能还有空格或制表符应加判断  
  37.        while(' '==s[i]||'/t'==s[i])  
  38.        {  
  39.               i++;  
  40.        }  
  41.        sign = ('-'==s[i])?-1:1;  
  42.        if('-'==s[i]||'+'==s[i])  
  43.        {  
  44.               i++;  
  45.        }  
  46.        while(s[i]!='/0')  
  47.        {  
  48.               sum = s[i]-'0'+sum*10;  
  49.               i++;  
  50.        }      
  51.        return sign*sum;  
  52. }  
  53.    
  54.    
  55. 3.//字符串拷贝函数  
  56. #include "stdafx.h"  
  57. #include <assert.h>  
  58. #include <string.h>  
  59. #include <iostream>  
  60. using namespace std;  
  61. char *srcpy(char *dest,const char *source)  
  62. {  
  63.        assert((dest!=NULL)&&(source!=NULL));  
  64.        char *address = dest;  
  65.        while(*source!='/0')  
  66.        {  
  67.               *dest++=*source++;  
  68.        }  
  69.        *dest = '/0';  
  70.        return address;  
  71. }  
  72.    
  73. 4.//判断输入的是否是一个回文字符串  
  74. #include "stdafx.h"  
  75. #include <string.h>  
  76. #include <iostream>  
  77. using namespace std;  
  78. //方法一:借助数组  
  79. bool isPalindrome(char *input)  
  80. {  
  81.        char s[100];  
  82.        strcpy(s,input);  
  83.        int length = strlen(input);  
  84.        int begin = 0,end = length-1;  
  85.        while(begin<end)  
  86.        {  
  87.               if(s[begin]==s[end])  
  88.               {  
  89.                      begin++;  
  90.                      end--;  
  91.               }  
  92.               else  
  93.               {  
  94.                      break;  
  95.               }             
  96.        }  
  97.        if(begin<end)  
  98.        {  
  99.               return false;  
  100.        }      
  101.        else  
  102.        {  
  103.               return true;  
  104.        }        
  105. }  
  106. //方法二:使用指针  
  107. bool isPalindrome2(char *input)  
  108. {  
  109.        if(input==NULL)  
  110.               return false;  
  111.        char *begin = input;  
  112.        char *end = begin+strlen(input)-1;  
  113.        while(begin<end)  
  114.        {  
  115.               if(*begin++!=*end--)  
  116.                      return false;  
  117.        }  
  118.        return true;  
  119. }  
  120.    
  121. int main(int argc, char* argv[])  
  122. {  
  123.        char *s ="1234554321";  
  124.        if(isPalindrome(s))  
  125.        {  
  126.               cout<<"True"<<endl;  
  127.        }  
  128.        else  
  129.        {  
  130.               cout<<"Fasle"<<endl;  
  131.        }  
  132.    
  133.        if(isPalindrome2(s))  
  134.        {  
  135.               cout<<"True"<<endl;  
  136.        }  
  137.        else  
  138.        {  
  139.               cout<<"Fasle"<<endl;  
  140.        }  
  141.        cin.get();  
  142.    
  143.        return 0;  
  144. }  
  145.    
  146.    
  147. 5.//不使用库函数,编写函数int strcmp(char *source, char *dest),若相等返回0,否则返回-1  
  148. int strcmp(char *source, char *dest)  
  149. {  
  150.        assert(source != NULL && dest != NULL);  
  151.        while(*source++==*dest++)  
  152.        {  
  153.               if(*source=='/0'&&*dest=='/0')  
  154.                      return 0;          
  155.        }  
  156.        return -1;  
  157. }  
 

[c-sharp]  view plain copy
  1. #include <stdio.h>  
  2. void strcat(char *string1, char *string2){  
  3.     while(*string1 != '/0')  
  4.         string1++;  
  5.     while(*string2)  
  6.     {  
  7.         *string1++ = *string2++;  
  8.     }  
  9.     *string1++ = '/0';  
  10. }  
  11. int strlen(char *string1){  
  12.     int count = 0;  
  13.     while(*string1++ != '/0')  
  14.         count++;  
  15.     return count;  
  16. }  
  17. int main(void)  
  18. {  
  19.     char name[100]="wangfeng";  
  20.     char *mesg = " is a student!";  
  21.     strlen(name);  
  22.     puts(name);  
  23.     return 0;  
  24. }  
  25. #include <stdlib.h>  
  26. /* 
  27.    这个函数调用的是库函数中的 
  28.    strtol()函数,关于这个函数的 
  29.    源代码后面将会给出。 
  30. */  
  31. int my_atoi(char *str)  
  32. {  
  33.    return (int) strtol(str, NULL, 10);  
  34. }  
  35. /* 
  36.    下面的两个函数没有调用strtol()函数, 
  37.    而是直接给出了该函数的实现。 
  38. */  
  39. int my_atoi01(const char *str)  
  40. {  
  41.    long int v=0;  
  42.    int sign = 0;  
  43.    
  44.    while ( *str == ' ')  str++;  
  45.    
  46.    if(*str == '-'||*str == '+')  
  47.       sign = *str++;  
  48.    
  49.    while (isdigit(*str))  
  50.    {  
  51.       v = v*10 + *str - '0';  
  52.       str++;  
  53.    }  
  54.    return sign == '-' ? -v:v;  
  55. }  
  56. int my_atoi02(char *str)  
  57. {  
  58.    int sign;  
  59.    int n;  
  60.    unsigned char *p;  
  61.    
  62.    p=str;  
  63.    while (isspace(*p) ) p++;  
  64.    
  65.    sign = (*p == '-' ) ? -1 : 1;  
  66.    
  67.    if (*p=='+' || *p=='-' ) p++;  
  68.    
  69.    for (n=0; isdigit(*p); p++)  
  70.       n = 10*n + (*p - '0');  
  71.    
  72.    return sign*n;  
  73. }  
  74.    
  75. int main()  
  76. {  
  77.    char * str = "2147483647";  
  78.    printf("%d/n",my_atoi(str));  
  79.     
  80.    str = "-2147483648";  
  81.    printf("%d/n",my_atoi(str));  
  82.     
  83.    str = "2147483647";  
  84.    printf("%d/n",my_atoi01(str));  
  85.     
  86.    str = "-2147483648";  
  87.    printf("%d/n",my_atoi01(str));  
  88.     
  89.    str = "2147483647";  
  90.    printf("%d/n",my_atoi02(str));  
  91.     
  92.    str = "-2147483648";  
  93.    printf("%d/n",my_atoi02(str));  
  94.     
  95.    system("pause");  
  96.    return 0;  
  97. }  


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值