Reverse String ----LeetCode

来源:互联网 发布:sem高级优化师认证 编辑:程序博客网 时间:2024/06/11 05:28

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".


C++:

#include <iostream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
string s;
cin>>s;
int left=0;
int right=s.length()-1;
for(;left<right;left++,right--)
{
swap(s[left],s[right]);
}
cout<<s<<endl;
system("PAUSE");
return 0;
}
void swap(char a,char b)
{
char temp;
temp=a;
a=b;
b=a;
}


0 0
原创粉丝点击