Vulkan

Cracking The Coding Interview 1.2

//原文:
//
//	Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
//
//	从前向后交换,到中间为止

#include <iostream>
using namespace std;
void mSwap(char &a, char &b)
{
	char c=a;
	a=b;
	b=c;
}
void mReverse(char *str)
{
	if (str == NULL)
	{
		return;
	}
	int size = strlen(str);
	for (int i =0;i< size/2; i++)
	{
		mSwap(str[i], str[size-1-i]);
	}
}
int main()
{
	char s[] = "abcdefg";
	cout << s <<endl;
	mReverse(s);
	cout << s <<endl;
	return 0;
}

posted on 2014-03-31 13:44  Vulkan  阅读(153)  评论(0编辑  收藏  举报

导航