字符串倒置

来源:互联网 发布:家具画图软件 编辑:程序博客网 时间:2024/05/01 16:39

C语言很初级的题目,看到有人提问,就复习下巴

vs2010测试通过。

vs2010里面strlen不可用,索性自己写一个好了。


#include<stdio.h>//计算字符串长度int strlen(char* pstr){int count = 0;while(*pstr++!='\0'){count++;}return count;}//异或元算可逆原则:a^b=c ==> c^b=a//但是: a^a = 0,a的值不能等于b的值void swap(char *a, char *b){if(a!=b && *a!=*b){*a = *a^*b;*b = *a^*b;*a = *a^*b;}}int main(){char str[10]="hello";char *p;char *q;p = str;q = str+strlen(str)-1;while(p<=q){swap(p++,q--);}printf("%s",str);return 1;}


原创粉丝点击