加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

2.倒置字符串(这里第二个倒置出现问题不知道怎么解决)

发布时间:2020-12-13 23:04:29 所属栏目:百科 来源:网络整理
导读://倒置字符串#include iostreamusing namespace std;//倒置原字符串void reverse1(char * s){if (s){char temp;char * end=s;while (*end)//记住这里end只表示一个字符{end++;}--end;while (send){temp =*s;*s++=*end;*end-- =temp;}}}//返回个倒置字符串,
//倒置字符串
#include <iostream>
using namespace std;

//倒置原字符串
void reverse1(char * s)
{
	if (s)
	{
		char temp;
		char * end=s;
		while (*end)//记住这里end只表示一个字符
		{
			end++;
		}
		--end;
		while (s<end)
		{
			temp =*s;
			*s++=*end;
			*end-- =temp;
		}
	}
}
//返回个倒置字符串,不改变原字符串
char* reverse2(char * s,int m)
{
	if(s)
	{
		char *temp =new char[m+1];//没有delete以后再看吧
		//char temp[m+1]="0";
		for (auto i =0;i<m;i++)
		{
			temp[i] = s[m-i-1];
		}
		temp[m] ='';
		return temp;
	}
	return s;
}
//倒着显示字符串
//递归
void show(char *s)
{
	char temp;
	if(*s)
	{
		temp =*s++;
		show(s);
		cout<<temp;
	}
}
//不递归 用栈做  原理一样
void main()
{
	char s[11] ="1234567890";
	show(s);
	cout<<endl<<s<<endl;
	char *s1 =reverse2(s,strlen(s));
	for (auto i =0;i<10;i++)
	{
		cout<<*s1++;
	}
	cout<<endl;
	reverse1(s);
	cout<<s<<endl;
	system("pause");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读