16,548
社区成员




#include "stdafx.h"
BOOL StringReverseW(PWSTR pWideCharStr, DWORD cchLength);
int _tmain(int argc, _TCHAR* argv[])
{
PWSTR pWideCharStr = _T("String");
StringReverseW(pWideCharStr,8);
printf("StringReverseW the pWideCharStr: %s",pWideCharStr);
return 0;
}
//宽字节字符串颠倒算法
BOOL StringReverseW(PWSTR pWideCharStr, DWORD cchLength) {
// Get a pointer to the last character in the string. cchLength:最大允许的字节数
PWSTR pEndOfStr = pWideCharStr + wcsnlen_s(pWideCharStr , cchLength) - 1;
wchar_t cCharT;
// Repeat until we reach the center character in the string.
while (pWideCharStr < pEndOfStr) { //我不知道为什么是这么判断,感觉这样不对呀?
// Save a character in a temporary variable.
cCharT = *pWideCharStr;
// Put the last character in the first character.
*pWideCharStr = *pEndOfStr; //调式时候,这里会出现异常
// Put the temporary character in the last character.
*pEndOfStr = cCharT;
// Move in one character from the left.
pWideCharStr++;
// Move in one character from the right.
pEndOfStr--;
}
// The string is reversed; return success.
return(TRUE);
}