//--------字符串转换为整数
int myAtoi(const char* s){- ]7 ~; ?" F) ~4 b- a1 N
int result = 0;
int flag = 1; K& D+ `7 /% r: V( n. I
int i = 0;& Y5 Z/ l1 k4 w5 ~" i
while(isspace(s))
i++;
if(s == '-'){
flag = -1;8 A- I9 e* T+ R/ u8 H$ |+ n' p. J
i++;
}9 r( P7 H, i6 X" q6 }6 H
if(s == '+')
i++;
while(s != '/0'){- b* h9 @, D4 d
if((s > '9') || (s < '0'))
break;8 g2 l) _- D- ~+ x2 u
int j = s - '0';, d: C! n$ G2 j5 `% g
result = 10 * result + j;
i++;& [; B( Z( |3 T" c- y, t& ~
}% I' r3 E8 E# j
result = result * flag;- W# c# Z' R3 x J# ~3 f
return result;
}
========================================
//--------整数转换为字符串
void itoa (int n,char s[])+ R: `' f8 z* y) _
{
int i,j,sign;
if((sign=n)<0)//记录符号
n=-n;//使n成为正数. `2 @" a% L m
i=0;4 P5 x6 W' i7 O# B# c
do{
s[i++]=n%10+'0';//取下一个数字! { v K' }. ^3 X3 D1 O2 p
}while ((n/=10)>0);//删除该数字# q, T/ o/ T2 v# A A
if(sign<0)
s[i++]='-';! [2 h; |( U4 R+ L P
s='/0';1 N- a6 T7 B4 }' ]5 x2 _" R
for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出
printf("%c",s[j]);! e% |6 r: Y' l* e" r; @* T
printf("/n");
}
/* 冒泡排序 */
void bubble(int *array, int num)
{
int i, j, t;
for(i = 0; i < num -1; i++)
{
for(j = 0; j < num - i - 1 ; j++)
{
if(array[j] > array[j+1])
{
array[j] ^= array[j+1];
array[j+1] ^= array[j];
array[j] ^= array[j+1];
/*
t = array[j];
array[j] = array[j + 1];
array[j + 1] = t;
*/
}
}
}
return;
}
[cpp] view plaincopyprint?
/******************************************************************************
* author : kangquan@scut2008
*
* blog : http://blog.csdn.net/kangquan2008
*
* discription: 自定义实现字符串处理函数
* size_t strlen (const char * str);
* int strcmp (const char * src,const char * dst);
* char * strcat (char * dst, const char * src);
* char * strcpy(char * dst, const char * src);
* char * strncat (char * front,const char * back, size_t count);
* char * strncpy (char * dest, const char * source, size_t count);
* char * strchr (const char * string,int ch);
* char * strstr (const char * str1, const char * str2);
*
*****************************************************************************/
#include
#include
size_t strlen (const char * str)
{
const char * eos = str;
while( *eos++ );
// *eos == '\0' 时,还加1,因为strlen是不包含'\0'的,
// 所以下面-1.
return (eos - str - 1);// 不包含 '\0'
}
// ugly implement
// 这个实现不是很好,首先没考虑const,然后返回值为有符号整数
int my_strlen ( char * str )
{
int len = 0;
while( *str++ )
len++;
return len;
}
int strcmp (const char * src,const char * dst)
{
int ret;
// 转为unsigned char,好处是?
// 另外只判断 *dst != 0,因为此时已经有*src == *dst
while( !(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst )
src++,dst++;
if(ret < 0)
ret = -1;
else if(ret > 0)
ret = 1;
return ret;
}
// ugly implement
// 这个实现并不好,因为没有考虑strcat嵌套使用的情况,好的实现见strcat
char * my_strcat (char * dst, const char * src)
{
while( *dst )
dst++; // 移到末尾
while( *dst++ = *src++);
*dst = '\0'; // 记得是*dst = '\0';而不是dst = '\0'
return dst;
}
char * strcat (char * dst, const char * src)
{
char * tmp = dst; // 设置临时变量,
while( *dst )
dst++; // 移到末尾
while( *dst++ = *src++);
*dst = '\0';
return tmp;
}
char * strcpy(char * dst, const char * src)
{
char * tmp = dst;
while( *dst++ = *src++ )
;
return tmp;
}
char * strncat (char * dst,const char * src, size_t count)
{
char * tmp = dst;
while( *dst )
dst ++;
while( count-- ) // 先返回,后自增.count到1,则count个
if( !( *dst++ = *src++ ) )
return tmp; // 已经发现'\0',直接返回,不要再加'\0'
*dst = '\0';
return tmp;
}
char * strncpy(char * dst, const char * src, size_t count)
{
char * tmp = dst;
while( count && (*dst++ = *src++) )
count --;
if( !count ) // 复制了count个,加上'\0'
*dst = '\0';
else{
while( --count ) // 在遇到'\0'且未复制够count个字符,补上'\0'
*dst++ = '\0';
}
return tmp;
}
// '\0'也可以找
char * strchr (const char * string,int ch)
{
// while( *string && *string++ != (char)ch ) // == 时还自增,这是不对的.
// ;
while (*string && *string != (char)ch)
string++;
if(*string == (char)ch)
return (char *)string;
return NULL;
}
char * strstr (const char * str1, const char * str2)
{
char * index = (char *)str1;
char *s1,*s2;
if( !*str2)
return (char *)str1; // 要查找的为空,直接返回.
while( *index )
{
s1 = index;
s2 = (char *)str2;
while( *s1 && *s2 && \
!(*(unsigned char *)s1-*(unsigned char *)s2))
s1++,s2++;
if( !*s2 )
return index;
index++;
}
return NULL;
}
int main()
{
char dst[] = "I come from ";
char dst2[] = "I come from ";
char src[] = "scut";
char * tmp = NULL;
char ch = 'c';
char str[] = "from";
printf("my_strlen: %d\n",my_strlen(dst));
printf("strlen: %d\n",strlen(dst));
my_strcat(dst,src);
printf("my_strcat: %s\n",dst);
printf("strcat: %s\n",strcat(dst,src));
printf("strncat: %s\n",strncat(dst,src,3));
printf("strcpy: %s\n",strcpy(dst,src));
printf("strncpy: %s\n",strncpy(dst,src,3));
printf("strchr: %s\n",strchr(dst,ch) ? strchr(dst,ch):"NULL" );
printf("strstr: %s\n",strstr(dst2,str)? strstr(dst2,str):"NULL" );
return 0;
}