实现宽字符串的逆转
//将空格处理成字符串结束标志 void set0(wchar_t wstr[20]) { wchar_t *p = wstr; while (*p != L'\0') { if (*p == L' ') { *p = L'\0'; } p++; } } //结束标志各位空格 void set1(wchar_t wstr[20]) { for (int i = 0; i < 7;i++) { if (wstr[i]==L'\0') { wstr[i] = ' '; } } } wchar_t * mywcsrev(wchar_t * _Str) { <span style="white-space:pre"> </span> wchar_t *phead = _Str; <span style="white-space:pre"> </span> wchar_t*pback = _Str + wcslen(_Str)-1; <span style="white-space:pre"> </span> while (phead<pback)//指针交换 <span style="white-space:pre"> </span> { <span style="white-space:pre"> </span> wchar_t ch = *phead; <span style="white-space:pre"> </span> *phead = *pback; <span style="white-space:pre"> </span> *pback = ch; <span style="white-space:pre"> </span> phead++; <span style="white-space:pre"> </span> pback--; <span style="white-space:pre"> </span> } } //wcsrev //宽字符串反转 void main() { setlocale(LC_ALL, "zh-CN"); wchar_t wstr[20] = L"黑夜 要 飞啦"; wprintf(L"%c %c\n%s\n", wstr[0], wstr[6], wstr); set0(wstr);//切割 去除空格 mywcsrev(wstr);// mywcsrev(wstr+5); set1(wstr);//换回空格 mywcsrev(wstr); wprintf(L"%ls\n", wstr); system("pause"); }
版权声明:本文为博主原创文章,欢迎指出代码不良之处,提出代码优化方案。欢迎指点,黑夜代码,拼命更新,努力奋斗中......
时间: 2024-10-26 19:19:26