[cc150] 1.2
1668 ワード
1.2 Write code to reverse a C-Style String.
このテーマにも何か特別なことがありますが、注意しなければならないのは文字列の定義方法です.
char*str="hello"はコードセグメントに置かれており、char str[10]="hello"はスタックに置かれているので、任意に使用できますが、アドレスが境界を越えないように注意してくださいね
- class Solution {
- public:
- void reverse(char *str){
- if (!*str) return;
- char *p = str, *q = str;
- while (*q) q++;
- q--;
- char tmp;
- while (p < q) {
- tmp = *p;
- *p++ = *q;
- *q-- = tmp;
- }
- }
- };
このテーマにも何か特別なことがありますが、注意しなければならないのは文字列の定義方法です.
char*str="hello"はコードセグメントに置かれており、char str[10]="hello"はスタックに置かれているので、任意に使用できますが、アドレスが境界を越えないように注意してくださいね