Wednesday, January 6, 2010

Reverse a string C/C++

#include < iostream >
#include < string >

using namespace std;

void reverseStr(string& x)
{
string temp = "";

int len = x.length();

for(int i = len - 1; i >= 0; --i)
{
temp += x[i];
}

x = temp;
}

int main()
{
string x = "Hello World!";

cout << "x = " << x << endl;

reverseStr(x);

cout << "x = " << x << endl;

return 0;
}

No comments:

Post a Comment