1、strcat//将指针指向的字符串放到目的字符串后
[cpp]
char *strcat(char *strDest, const char *strScr) //将源字符串加const,表明其为输入参数
{
char * address = strDest; //该语句若放在assert之后,编译出错
assert((strDest != NULL) && (strScr != NULL)); //对源地址和目的地址加非0断言
while(*strDest) //是while(*strDest!='\0')的简化形式
{ //若使用while(*strDest++),则会出错,因为++是不受循环
strDest++; //约束的。所以要在循环体内++;因为要是*strDest最后指
} //向该字符串的结束标志’\0’。
while(*strDest++ = *strScr++) //是while((*strDest++ = *strScr++)!='\0')的简化形式
{
NULL; //该循环条件内可以用++,
} //此处可以加语句*strDest='\0';有无必要?
return address; //为了实现链式操作,将目的地址返回
}
char *strcat(char *strDest, const char *strScr) //将源字符串加const,表明其为输入参数
{
char * address = strDest; //该语句若放在assert之后,编译出错
assert((strDest != NULL) && (strScr != NULL)); //对源地址和目的地址加非0断言
while(*strDest) //是while(*strDest!='\0')的简化形式
{ //若使用while(*strDest++),则会出错,因为++是不受循环
strDest++; //约束的。所以要在循环体内++;因为要是*strDest最后指
} //向该字符串的结束标志’\0’。
while(*strDest++ = *strScr++) //是while((*strDest++ = *strScr++)!='\0')的简化形式
{
NULL; //该循环条件内可以用++,
} //此处可以加语句*strDest='\0';有无必要?
return address; //为了实现链式操作,将目的地址返回
}
2、strcmp//字符串比较函数
int __cdecl 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);
}
int __cdecl 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);
}
[cpp]
int strcmp(const char *dest, const char *source)
{
assert((NULL != dest) && (NULL != source));
while (*dest && *source && (*dest == *source))
{
dest ++;
source ++;
}
return *dest - *source;
/*如果dest > source,则返回值大于0,如果dest = source,则返回值等于0,如果dest < source ,则返回值小于0。*/
}
int strcmp(const char *dest, const char *source)
{
assert((NULL != dest) && (NULL != source));
while (*dest && *source && (*dest == *source))
{
dest ++;
source ++;
}
return *dest - *source;
/*如果dest > source,则返回值大于0,如果dest = source,则返回值等于0,如果dest < source ,则返回值小于0。*/
}
3、strcpy//字符串拷贝函数
[cpp]
char *strcpy(char *strDestination,const char *strSource)
{
assert(strDestination!=NULL && strSource!=NULL);
char *strD=strDestination;
while ((*strD++=*strSource++)!='\0');
return strDestination;
}
char *strcpy(char *strDestination,const char *strSource)
{
assert(strDestination!=NULL && strSource!=NULL);
char *strD=strDestination;
while ((*strD++=*strSource++)!='\0');
return strDestination;
}
4、strncmp//比较字符串前size个字符
[cpp]
char *strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;
assert(src!=NULL && dest!=NULL);
while (count) {
if ((*tmp = *src) != 0)
src++;
tmp++;
count--;
}
return dest;
}
char *strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;
assert(src!=NULL && dest!=NULL);
while (count) {
if ((*tmp = *src) != 0)
src++;
tmp++;
count--;
}
return dest;
}
5、strrchr//查找某字符在字符串中首次出现的位置
[cpp]
char *
strrchr (const char *s, int c)
{
register const char *found, *p;
c = (unsigned char) c;
/* Since strchr is fast, we use it rather than the obvious loop. */
if (c == '\0')
return strchr (s, '\0');
found = NULL;
while ((p = strchr (s, c)) != NULL)
{
found = p;
s = p + 1;
}
return (char *) found;
}
char *
strrchr (const char *s, int c)
{
register const char *found, *p;
c = (unsigned char) c;
/* Since strchr is fast, we use it rather than the obvious loop. */
if (c == '\0')
return strchr (s, '\0');
found = NULL;
while ((p = strchr (s, c)) != NULL)
{
found = p;
s = p + 1;
}
return (char *) found;
}
6.Strlen函数原型如下:
int strlen(const char *str)
{
int len = 0;
assert(str != NULL);
while(*str++)
{
len++;
}
return len;
}
以下是在VC6.0中调试的例子,函数名用strlena代替。
#include <stdio.h>
#include <assert.h>
int strlena(const char *str)
{
int len = 0;
assert(str != NULL);
while(*str++)
{
len++;
}
return len;
}
void main()
{
char str1[100] = {"i love"};
char str2[50] = {"China "};
printf("%d\n",strlena(str1));
}
#include<stdlib.h>
const char *strstr_arr( char const *str1,char const *str2)
{
int i=0;
for( i=0; str1[i]!='\0'; i++)
{
int tmp=i;
int j=0;
while(str1[i++]==str2[j++])
{
if(str2[j]=='\0')
{
return &str1[tmp];
}
}
i=tmp;
}
return NULL;
}
int main()
{
char line[]="abcdefgh";
char arr[]="cde";
printf("%s\n",strstr_arr(line,arr));
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
void reverse_arr(char a[],int sz)
{
int i=0;
char *pa=NULL;
for(; i<sz; i++)
{
pa=&a[sz-i-1];
printf("%c ",*pa);
}
}
int main()
{
char a[]="abcdef";
int sz=sizeof(a)/sizeof(a[0]);
reverse_arr(a,sz);
//printf("%s\n",a);
system("pause");
return 0;
}