字符串逆序输出的简单算法

// Reverse.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

void Reverse(char* a,int b,int e)
{
	for(;b<e;b++,e--)
	{
		char temp;
		temp = a[b];
		a[b]=a[e];
		a[e]=temp;
	}
}


int _tmain(int argc, _TCHAR* argv[])
{
	char a[]="abcd";
	Reverse(a,0,3);
	cout<<a<<endl;
	system("pause");
	return 0;
}

其实通过字符指针实现字符串的翻转的代码很简单:

代码如下:

 

void Reverse(char* str)
{
	char* begin=str;
	char* end = str;
	while(*end!='\0')
		end++;
	end--;
	while(begin<end)
	{
		char temp;
		temp=*begin;
		*begin=*end;
		*end=temp;
		begin++;
		end--;
	}

}




 

你可能感兴趣的:(算法)