源码:微软strcpy,strcat和strcmp的实现源代码 itoa atoi

转载 2011年01月10日 22:48:00

/***

*char *strcpy(dst, src) - copy one string over another

*

*Purpose:

* Copies the string src into the spot specified by

* dest; assumes enough room.

*

*Entry:

* char * dst - string over which "src" is to be copied

* const char * src - string to be copied over "dst"

*

*Exit:

* The address of "dst"

*

*Exceptions:

*******************************************************************************/

char* strcpy(char * dst, const char * src)

{

char * cp = dst;

while( *cp++ = *src++ )

; /* Copy src over dst */

return( dst );

}

/***

*char *strcat(dst, src) - concatenate (append) one string to another

*

*Purpose:

* Concatenates src onto the end of dest. Assumes enough

* space in dest.

*

*Entry:

* char *dst - string to which "src" is to be appended

* const char *src - string to be appended to the end of "dst"

*

*Exit:

* The address of "dst"

*

*Exceptions:

*

*******************************************************************************/

Char* strcat ( char * dst , const char * src )

{

char * cp = dst;

while( *cp )

cp++; /* find end of dst */

while( *cp++ = *src++ ) ; /* Copy src to end of dst */

return( dst ); /* return dst */

}

/***

*strcmp - compare two strings, returning less than, equal to, or greater than

*

*Purpose:

* STRCMP compares two strings and returns an integer

* to indicate whether the first is less than the second, the two are

* equal, or whether the first is greater than the second.

*

* Comparison is done byte by byte on an UNSIGNED basis, which is to

* say that Null (0) is less than any other character (1-255).

*

*Entry:

* const char * src - string for left-hand side of comparison

* const char * dst - string for right-hand side of comparison

*

*Exit:

* returns -1 if src < dst

* returns 0 if src == dst

* returns +1 if src > dst

*

*Exceptions:

*

*******************************************************************************/

int strcmp ( const char* src, const char* dst )

{

int ret = 0 ;

while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)

++src, ++dst;

if ( ret < 0 )

ret = -1 ;

else if ( ret > 0 )

ret = 1 ;

return( ret );

}

 itoa atoi

void itoa(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';
}


int atoi(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;
} 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/csw_100/archive/2010/08/24/5836079.aspx

相关文章推荐

strcpy,strcat, strcmp, atoi, itoa函数原型实现

基础函数的实现(strcpy,strcat, strcmp, atoi, itoa)2010-10-09 17:33转载自 mickeysullivan最终编辑 mickeysullivanstrcp...

strcat、strcmp、strcpy、strncpy、memset、memcpy、memmove和atoi函数源码

char *strcat(char *strDest, const char *strSrc) //将源字符串加const,表明其为输入参数 { char *address = strDest; ...

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

C++常用库函数atoi,itoa,strcpy,strcmp的实现 . view plaincopy to clipboardprint? 01.1.//整数转换成字符串itoa函数的实现 ...

memcpy, strcpy, strlen, strcmp, itoa, atoi 的函数实现

不使用库函数,实现 memcpy, strcpy, strlen, strcmp, itoa, atoi. 本文地址 : 1. memcpy内存拷贝函数。 函数原型void *memcpy(voi...

部分字符串库函数的重写(源码) toupper,memcpy,memmove,memset,memchr,strlen,strcpy,strcat,strcmp,strchr

#include #include #include //////////////////////////////////////////////////// //Convert cha...

模拟实现strlen,strcpy,strcmp,strcat,strstr库函数

模拟实现strlen库函数 功能:返回字符串的长度。该字符串可能是自己定义的,也可能是内存中随机的,该函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符'\0'。返回的长...

strlen,strcat,strcpy,strncpy,strcmp的具体实现

strcpy函数: [cpp] view plaincopy char *strcpy(char *Dest , const char *Src)   {   ...

strlen strcmp strcat strcpy strncpy strstr strchar函数的实现

strlen的实现#include using namespace std; int my_strlen(const char * str) { int count = 0; while (*...

[C语言]strcat strstr strcpy strcmp strlen 的实现与使用

函数名:strcat  功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。 说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳s...

模拟实现C库函数strcpy,strcat,strstr,strchr,strcmp,memcpy,memmove

1、模拟实现strcpy char *my_strcpy( char *dest, const char *str) { assert(dest != NULL); assert(str != N...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:源码:微软strcpy,strcat和strcmp的实现源代码 itoa atoi
举报原因:
原因补充:

(最多只允许输入30个字)