C语言字符串操作函数
感觉挺实用的,在此转载引用下供以后学习用,在此感谢JCSU提供本文
http://blog.csdn.net/yanglituan5/article/details/6732579
1. 字符串反转 - strRev
2. 字符串复制 - strcpy
3. 字符串转化为整数 - atoi
4. 字符串求长 - strlen
5. 字符串连接 - strcat
6. 字符串比较 - strcmp
7. 计算字符串中的元音字符个数
8. 判断一个字符串是否是回文
1. 写一个函数实现字符串反转
void strRev(char *s)
{
char temp, *end = s + strlen(s) - 1;
while( end > s)
{
temp = *s;
*s = *end;
*end = temp;
--end;
++s;
}
}
版本2 - for版
void strRev(char *s)
{
char temp;
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
temp = *s;
*s = *end;
*end = temp;
}
}
版本3 - 不使用第三方变量
{
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
*s ^= *end;
*end ^= *s;
*s ^= *end;
}
}
版本4 - 重构版本3
void strRev(char *s)
{
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
*s ^= *end ^= *s ^= *end;
}
}
版本5 - 重构版本4
{
for(char *end = s + strlen(s) - 1; end > s ; *s++ ^= *end ^= *s ^= *end--);
}
版本6 - 递归版
{
if(s[0] == '/0')
return;
else
strRev(&s[1]);
printf("%c",s[0]);
}
2. 实现库函数strcpy的功能
版本1
{
char *p=dest;
while(*dest++ = *src++)
;
dest=p;
}
版本2
{
char *p = dst;
while( *p ++ = *src ++ )
;
return dst;
}
版本3
{
int i=0;
for(; *(src+i)!='/0'; i++)
*(dest+i) = *(src+i);
*(dest+i) = '/0';
}
3. 实现库函数atoi的功能
版本1 - 附说明
{
if( 0 == exp )
return 1;
return base*power(base