字符串翻转

#include <iostream>
using namespace std;
//计算字符串长度
size_t str_len(char *str_source)
{
   size_t i = 0;
   while (*str_source++ != '\0')
   {
	   ++i;
   }
   return i;
}
//翻转
char* reverse_str(char* str_source)
{
	size_t len = str_len(str_source);
	char* str_new = new char[len+1];
	for (size_t i = 0; i != len; ++i)
	{
		str_new[i] = str_source[len-i-1];
	}
	str_new[len]='\0';
	return str_new;
}
int main()
{
	char* str_sourec = "sdjakabc";
	cout << reverse_str(str_sourec);
}
posted @ 2011-07-16 21:11  hailong  阅读(219)  评论(0编辑  收藏  举报