标签:
题目:
Write code to reverse a C-Style String (C-String means that “abcd” is represented as five characters, including the null character )
参考解法:把数组的思维去除,用指针的哨兵来做会更简单一点。
第一遍解:
遍历字符串得出长度后,然后首尾互换。
// // main.cpp // reverse // // Created by lexnewgate on 15/11/6. // Copyright © 2015年 lexnewgate. All rights reserved. // #include <iostream> #include <vector> void reverseStr(char*str) { int i=0; for (i=0; *(str+i)!=‘\0‘; i++) { } for (int j=0; j<i/2; j++) { char temp=*(str+j); *(str+j)=*(str+i-j-1); *(str+i-j-1)=temp; } return ; } int main(int argc, const char * argv[]) { char p[]="abcdcxdswsw"; reverseStr(p); printf("%s\n",p); return 0; }
标签:
原文地址:http://www.cnblogs.com/AlexWei/p/4943331.html