Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8320551
  • 博文数量: 1413
  • 博客积分: 11128
  • 博客等级: 上将
  • 技术积分: 14685
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-13 10:03
个人简介

follow my heart...

文章分类

全部博文(1413)

文章存档

2013年(1)

2012年(5)

2011年(45)

2010年(176)

2009年(148)

2008年(190)

2007年(293)

2006年(555)

分类: C/C++

2009-02-23 16:20:38

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


1.//整数转换成字符串itoa函数的实现

#include "stdafx.h"

#include

using namespace std;

void itoaTest(int num,char str[] )

{

       int sign = num,i = 0,j = 0;

       char temp[11];

       if(sign<0)//判断是否是一个负数

       {

              num = -num;

       };

       do

       {

              temp[i] = num%10+'0';        

              num/=10;

              i++;

       }while(num>0);

       if(sign<0)

       {

              temp[i++] = '-';

       }

       temp[i] = '\0';

       i--;

       while(i>=0)

       {

              str[j] = temp[i];

              j++;

              i--;

       }

       str[j] = '\0';

}

2. //字符串转换成整数atoi函数的实现

int atoiTest(char s[])

{

       int i = 0,sum = 0,sign;    //输入的数前面可能还有空格或制表符应加判断

       while(' '==s[i]||'\t'==s[i])

       {

              i++;

       }

       sign = ('-'==s[i])?-1:1;

       if('-'==s[i]||'+'==s[i])

       {

              i++;

       }

       while(s[i]!='\0')

       {

              sum = s[i]-'0'+sum*10;

              i++;

       }    

       return sign*sum;

}

 

 

3.//字符串拷贝函数

#include "stdafx.h"

#include

#include

#include

using namespace std;

char *srcpy(char *dest,const char *source)

{

       assert((dest!=NULL)&&(source!=NULL));

       char *address = dest;

       while(*source!='\0')

       {

              *dest++=*source++;

       }

       *dest = '\0';

       return address;

}

 

4.//判断输入的是否是一个回文字符串

#include "stdafx.h"

#include

#include

using namespace std;

//方法一:借助数组

bool isPalindrome(char *input)

{

       char s[100];

       strcpy(s,input);

       int length = strlen(input);

       int begin = 0,end = length-1;

       while(begin

       {

              if(s[begin]==s[end])

              {

                     begin++;

                     end--;

              }

              else

              {

                     break;

              }           

       }

       if(begin

       {

              return false;

       }    

       else

       {

              return true;

       }      

}

//方法二:使用指针

bool isPalindrome2(char *input)

{

       if(input==NULL)

              return false;

       char *begin = input;

       char *end = begin+strlen(input)-1;

       while(begin

       {

              if(*begin++!=*end--)

                     return false;

       }

       return true;

}

 

int main(int argc, char* argv[])

{

       char *s ="1234554321";

       if(isPalindrome(s))

       {

              cout<<"True"<

       }

       else

       {

              cout<<"Fasle"<

       }

 

       if(isPalindrome2(s))

       {

              cout<<"True"<

       }

       else

       {

              cout<<"Fasle"<

       }

       cin.get();

 

       return 0;

}

 

 

5.//不使用库函数,编写函数int strcmp(char *source, char *dest),若相等返回0,否则返回-1

int strcmp(char *source, char *dest)

{

       assert(source != NULL && dest != NULL);

       while(*source++==*dest++)

       {

              if(*source=='\0'&&*dest=='\0')

                     return 0;        

       }

       return -1;

}


阅读(921) | 评论(1) | 转发(0) |
0

上一篇:TCP与UDP的区别

下一篇:C函数atoi的实现

给主人留下些什么吧!~~

chinaunix网友2009-02-26 17:23:37

脱裤子放屁