c语言 指针 字符串 回文 strcmp,C语言字符串操作函数

C语言字符串操作函数

版本1 - while版

copycode.gifvoidstrRev(char*s)

{chartemp,*end=s+strlen(s)-1;while( end>s)

{

temp=*s;*s=*end;*end=temp;--end;++s;

}

}

copycode.gif

版本2 - for版

copycode.gifvoidstrRev(char*s)

{chartemp;for(char*end=s+strlen(s)-1; end>s ;--end,++s)

{

temp=*s;*s=*end;*end=temp;

}

}

copycode.gif

版本3 - 不使用第三方变量

copycode.gifvoidstrRev(char*s)

{for(char*end=s+strlen(s)-1; end>s ;--end,++s)

{*s^=*end;*end^=*s;*s^=*end;

}

}

copycode.gif

版本4 - 重构版本3

copycode.gifvoidstrRev(char*s)

{for(char*end=s+strlen(s)-1; end>s ;--end,++s)

{*s^=*end^=*s^=*end;

}

}

copycode.gif

版本5 - 重构版本4

voidstrRev(char*s)

{for(char*end=s+strlen(s)-1; end>s ;*s++^=*end^=*s^=*end--);

}

版本6 - 递归版

copycode.gifvoidstrRev(constchar*s)

{if(s[0]=='\0')return;elsestrRev(&s[1]);

printf("%c",s[0]);

}

copycode.gif

2. 实现库函数strcpy的功能

strcpy函数位于头文件中

版本1

copycode.gifstrcpy(char*dest,constchar*src)

{char*p=dest;while(*dest++=*src++)

;

dest=p;

}

copycode.gif

版本2

copycode.gifchar*__cdecl strcpy(char*dst,constchar*src)

{char*p=dst;while(*p++=*src++)

;returndst;

}

copycode.gif

版本3

copycode.gifstrcpy(char*dest,constchar*src)

{inti=0;for(;*(src+i)!='\0'; i++)*(dest+i)=*(src+i);*(dest+i)='\0';

}

copycode.gif

3. 实现库函数atoi的功能

atoi函数位于头文件中

版本1 - 附说明

copycode.gifintpower(intbase,intexp)

{if(0==exp )return1;returnbase*power(base, exp-1);

}int__cdeclatoi(constchar*s)

{intexp=0, n=0;constchar*t=NULL;for(;*s==''||*s=='\t'||*s=='\n'; s++)//找到第一个非空字符;if(*s>'9'||*s='0'&&*t<='9';++t)//找到第一个非数字字符位置 - 方法1;

t--;/*找到第一个非数字字符位置 - 方法2

t=s;

while(*t++ >='0' && *t++ <='9')

;

t -= 2;*/while(t>=s)

{

n+=(*t-48)*power(10, exp);//数字字符转化为整数t--;

exp++;

}returnn;

}

copycode.gif

版本2

copycode.gifint__cdeclatoi(constchar*s)

{intexp=0, n=0;constchar*t=NULL;for(;*s==''||*s=='\t'||*s=='\n'; s++)//略过非空字符;if(*s>'9'||*s='0'&&*t<='9';++t)

;

t--;while(t>=s)

{

n+=(*t-48)*pow(10, exp);

t--;

exp++;

}returnn;

}

copycode.gif

4. 实现库函数strlen的功能

strlen函数位于头文件中

版本1 - while版

copycode.gifsize_t  __cdeclstrlen(constchar*s)

{inti=0;while(*s )

{

i++;

s++;

}returni;

}

copycode.gif

版本2 - for版

size_t  __cdeclstrlen(constchar*s)

{for(inti=0;*s; i++, s++)

;returni;

}

版本3 - 无变量版

copycode.gifsize_t  __cdeclstrlen(constchar*s)

{if(*s=='\0')return0;elsereturn(strlen(++s)+1);

}

copycode.gif

版本4 - 重构版本3

size_t __cdeclstrlen(constchar*s)

{return*s?(strlen(++s)+1) :0;

}

5. 实现库函数strcat的功能

strcat函数位于头文件中

版本1 - while版

copycode.gifchar*__cdeclstrcat(char*dst,constchar*src)

{char*p=dst;while(*p )

p++;while(*p++=*src++)

;returndst;

}

copycode.gif

6. 实现库函数strcmp的功能

strcmp函数位于头文件中

版本1 - 错误的strcmp

copycode.gifintstrcmp(constchar*a,constchar*b)

{for(;*a!='\0'&&*b!='\0'; a++, b++)if(*a>*b)return1;elseif(*a==*b)return0;elsereturn-1;

}

copycode.gif

版本2

copycode.gifint__cdeclstrcmp (constchar*src,constchar*dst)

{intret=0;while(!(ret=*(unsignedchar*)src-*(unsignedchar*)dst)&&*src)++src,++dst;if( ret<0)

ret=-1;elseif( ret>0)

ret=1;return( ret );

}

copycode.gif7. 计算字符串中元音字符的个数

copycode.gif#includeintis_vowel(chara)

{switch(a)

{case'a':case'A':case'e':case'E':case'i':case'I':case'o':case'O':case'u':case'U':return1;break;default:return0;break;

}

}intcount_vowel(constchar*s)

{intnum;if(s[0]=='\0')

num=0;else{if(is_vowel(s[0]))

num=1+count_vowel(&s[1]);elsenum=count_vowel(&s[1]);

}returnnum;

}intmain()

{char*s="AobCd ddudIe";

printf("%d \n", count_vowel(s));return0;

}

copycode.gif

8. 判断一个字符串是否回文:包含一个单词,或不含空格、标点的短语。如:Madam I'm Adam是回文版本1

copycode.gif/** 程序功能:判断一个单词,或不含空格、标点符号的短语是否为回文(palindrome)*/#include#includeintis_palindrome(constchar*s)

{boolis_palindrome=0;constchar*end=s;if(*end=='\0')/*如果s为空串,则是回文*/is_palindrome=1;while(*end)++end;/*end指向串s最后一个字符位置*/--end;while(s<=end)

{

while(*s==''||!isalpha(*s))/*略去串s中的非字母字符*/

++s;

while(*end==''||!isalpha(*end))

--end;

if(toupper(*s)==toupper(*end))/*将s中的字母字符转换为大字进行判断*/

{

++s;

--end;

}

else

{

is_palindrome=0;break;

} /*在s<=end的条件下,只要出现不相等就判断s不是回文*/

}

if(s>end)

is_palindrome=1;

else

is_palindrome=0;

return (is_palindrome);

}

int main()

{

constchar*s="Madam  I' m   Adam";

printf("%s %s \n", s, is_palindrome(s)?"is a palindrome!":"is not a palindrome!");

return0;

}

copycode.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值