指针练习
- 实现含有中文字符的字符串逆转,如: “我是小萌新” 转换成“新萌小是我”
#include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
void swap(unsigned char* str) {
int len = strlen((char*)str);
unsigned char tmp[256];
unsigned char* p1 = str;
unsigned char* p2 = tmp + len;
*p2-- = 0;
while (*p1) {
if (*p1 < 0x80) {
*p2-- = *p1++;
} else {
*(p2 - 1) = *p1++;
*p2 = *p1++;
p2 -= 2;
}
}
for (int i = 0; i < len; i++) {
str[i] = tmp[i];
}
}
int main(void) {
unsigned char str[64];
cout << "请输入: ";
cin >> str;
swap(str);
cout << str << endl;
system("pause");
return 0;
}