高手,请教一个字符分隔的问题?

tips020 2013-05-20 06:04:43
将一个 CString 接两位数 切割 后换位组合成新 CString

例如

CString A = "D23475B1";

需要换成下面这种格式,两位组合后

CString B = "B17534D2";

希望高手帮帮忙

CString 个数不限 但一定是双数的
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
starytx 2013-05-21
  • 打赏
  • 举报
回复
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);
    
}
赵4老师 2013-05-21
  • 打赏
  • 举报
回复
#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;
}
赵4老师 2013-05-21
  • 打赏
  • 举报
回复
#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;
}
hugett 2013-05-21
  • 打赏
  • 举报
回复

#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;
}
图灵狗 2013-05-20
  • 打赏
  • 举报
回复

#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;
}

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧