栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

2021-10-05 C++反转串的多种方法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-10-05 C++反转串的多种方法

#include 
#include 
#include 
#include 
using namespace std;

void reverse1(char *ch, int n)
{
	int low = 0, high = n-1;
	while(low < high)
	{
		swap(ch[low],ch[high]);
		low++;
		high--;
	}
}

void reverse2(char *ch, int n)
{
	for (int i = 0; i < n/2; ++i)
	{
		swap(ch[i],ch[n-1-i]);
	}
}

int reverseNum(int num)
{
	int result = 0;
	while(num)
	{
		result = result * 10 + num % 10;
		num = num / 10;
	}
	return result;
	cout << result;
}

int main(int argc, char const *argv[])
{
    char ch[] = "abcdef";

    cout << "1.原字符串:"<< ch << endl;

    cout << "2.while 循环翻转后:";
	reverse1(ch,strlen(ch));	cout << ch<< endl;

	cout << "3.for 循环又翻转回来后:";
	reverse2(ch,strlen(ch)); 	cout << ch << endl;

	cout << "4.string 的strrev()函数翻转回来:";
	strrev(ch);				 	cout << ch << endl;

	cout << "5.algorithm 的reverse()函数翻转回来:";
	string str = ch;
	reverse(str.begin(), str.end());	cout << str << endl;

	cout << "6.数字翻转123456:";
	int num = 123456;
	cout << reverseNum(num) << endl;

	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/297185.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号