fqy131314

1. 实现含有中文字符的字符串逆转,如: “我是小萌新” 转换成“新萌小是我”(指针项目练习1)

1. 实现含有中文字符的字符串逆转,如: “我是小萌新” 转换成“新萌小是我”

#include <iostream>
#include <Windows.h>
#include <string.h>

using namespace std;

void reverse(unsigned char *a)
{
	unsigned char *p1=a;
	unsigned char tmp[1024];
	unsigned char *p2;
	unsigned int len=strlen((char *)a);
	p2=len+tmp;//传的是地址,等式左边不能是指针(p2连接两个tmp+len的地址,所有说改变p2的值的话,tmp的值会跟着改变)

	*p2--='\0';
	while(*p1)
	{
		if(*p1<0x80) //代表一个字符
		{
			*p2--=*p1++;
		}else //代表中文字符
		{
			*(p2-1)=*p1++;
			*p2=*p1++;
			p2=p2-2;
		}		
	}

	for(int i=0;i<len;i++)
	{
		a[i]=tmp[i];
	}
}

int main(void)
{
	unsigned char str[1024];
	
	cout<<"请输入你要转换的字符:";
	cin>>str;

	reverse(str);

	
	cout<<"转换的结果为:"<<str<<endl;

	system("pause");
	return 0;
}

posted on 2022-11-03 09:58  会飞的鱼-blog  阅读(17)  评论(0)    收藏  举报  来源

导航