64,642
社区成员
发帖
与我相关
我的任务
分享
void reverStr(const CString &strSrc ,CString &strDes)
{
if (strSrc.GetLength() == 0)
{
return;
}
size_t nLen = strSrc.GetLength();
char *pStr = new char[nLen+1];
strncpy(pStr ,strSrc ,nLen);
char *p1 = pStr;
char *p2 = pStr+nLen-2;
char c;
while (p1 < p2)
{
c = *p1;
*p1++ = *p2;
*p2++ = c;
c = *p1;
*p1++ = *p2;
*p2 = c;
p2 -= 3;
}
*(pStr+nLen) = 0;
strDes = pStr;
delete []pStr;
}
// 调用示例
{
CString str("dfzc4f6h8d");
CString sTemp ;
reverStr(str ,sTemp);
AfxMessageBox(sTemp);
}
#include <afxdisp.h>
#include <stdio.h>
int main () {
CString A = "0123456789ABCDEF";
int L=A.GetLength();
CString B = "";
for (int i=L-2;i>=0;i-=2) B=B+A.Mid(i,2);
printf("%s\n",B);//EFCDAB8967452301
return 0;
}
#include <afxdisp.h>
#include <stdio.h>
int main () {
CString A = "D23475B1";
CString B = A.Right(2)+A.Mid(4,2)+A.Mid(2,2)+A.Left(2);
printf("%s\n",B);//B17534D2
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
string fun(string s){
int l = s.size();
char *s1 = new char[l + 1];
strcpy(s1, s.c_str());
char *s2 = new char[l + 1];
int i, j;
for(i = l - 2, j = 0; i >=0; i -= 2, j += 2){
s2[j] = s1[i];
s2[j + 1] = s1[i + 1];
}
s2[l] = 0;
string ret(s2);
delete [] s1;
delete [] s2;
return ret;
}
int main(){
string s = "D23475B1";
cout<<fun(s)<<endl;
return 0;
}
#include <stdio.h>
char* strrev2(char* s)
{
char* h = s;
char* t = s;
char ch;
while(*t++){};
t--;
t--;
while(h < t)
{
ch = *h;
*h = *(t - 1);
*(t - 1) = ch;
ch = *(h + 1);
*(h + 1) = *t;
*t = ch;
h += 2;
t -= 2;
}
return(s);
}
int main(void)
{
char str[] = "D23475B1";
printf("%s\n", strrev2(str));
return 0;
}