两种字符串逆序的方法
#include<iostream> #include<map> #include<algorithm> #include<numeric> using namespace std; void helper(char* str) ///用指针实现,注意strlen的用法,并且没有动最后的\0 { int len = strlen(str); int pleft = 0; int pright = len - 1; while(pleft < pright) { swap(str[pleft],str[pright]); pleft++; pright--; } printf("%s",str); } void helper1(char* str) ///用异或运算实现的,注意方法 { char* r = str; char* p = str; while(*(p+1) != '\0') ++p; while(p>r) { *p = *p^*r; *r = *p^*r; *p = *p--^*r++; } printf("%s",str); } int main() { // char* s = "abcdefg"; char s[] = "abcdefg"; helper(s); }
berkeleysong
posted on 2014-05-21 13:22 berkeleysong 阅读(170) 评论(0) 收藏 举报