|
hum... bad habits lead to bad code...
you're using C++, so use it entierely.
first of all, abandon the C-style strings (char*), it's just a pain to use when you can make use of the string class.
here is another solution:
std::string ReverseString(const std::string& str) {
std::string strTmp;
std::string::const_reverse_iterator iter;
for (iter = str.rbegin(); iter != rend(); --iter) {
strTmp += *iter;
}
return strTmp;
}
you could also use the swap technic:
std::string ReverseString(const std::string& str) {
std::string strTmp = str;
size_t len = strTmp.length();
for (size_t i = 0; iter < len/2; i++) {
strTmp[i] = str[len-i];
strTmp[len-i] = str[i];
}
return strTmp;
}
or even more powerful, using the STL algorithms (thanks jijo raj):
std::string ReverseString(const std::string& str) {
return std::reverse(str.begin(), str.end());
}
now in your main, just call it:
int main(int argc, char* argv[]) {
std::string s = "Hello World";
std::cout << "Before reversing : " << s <<endl;
std::cout << "After reversing : " << ReverseString(s) << endl;
}
modified on Wednesday, August 6, 2008 6:00 AM
|
|
|
|
|
You can further optimize it by using std::reverse() algorithm...
string Str = _T("Hello");
reverse( Str.begin(), Str.end());
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
damn it, i was sure such a functor was existing somewhere, but i couldn't put a hand on it though.
thanks for the notice.
|
|
|
|
|
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
Dosen't string have a reserve() function?
|
|
|
|
|
Naveen wrote: Dosen't string have a reserve() function?
reserve() ? what for ?
|
|
|
|
|
ops My bad. sorry I miss read the reserve as reverse.
|
|
|
|
|
|
Super Hornet wrote: Is the above code correct???
That all depends on what you were trying to accomplish. The use of cout appears to be the only thing C++ that I see.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Hi,
I have CListCtrl with 4 coloumn, I want to cerate CEdit and CButton control in 3 and 4 colomn. How can I do that?
And How can I handle these control on resize any colomn.
|
|
|
|
|
|
See here[^] and here[^]
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Hi all,
I want to check internet connection on particular pc... that is on which ever pc i run my code it should tell that net connection is there or not and on particular intervals it should check for internet connection....
How can i implement this through coding??
Thanks in advance
|
|
|
|
|
|
I tried using it, included intshcut.h header file but still linker is giving error netconnectDlg.obj : error LNK2001: unresolved external symbol __imp__InetIsOffline@4
Debug/netconnect.exe : fatal error LNK1120: 1 unresolved externals
how to remove this
|
|
|
|
|
|
I am using broadband connection and this api is always returning false(either net is connected or not)...
|
|
|
|
|
See here[^]
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
See here.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
I'm Chinese,So i don't speak English...
No Write!
|
|
|
|
|
|
There is you dont need to speak eng only write in english.
and it is already you done.
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH
|
|
|
|
|
SinBaby wrote: I'm Chinese,So i don't speak English...
Ho really?
I have a chinese to english translator installed in my eyes and hence I was able to read your post in english itself .
|
|
|
|
|
Naveen wrote: I have a chinese to english translator installed in my eyes and hence I was able to read your post in english itself
So maybe I can use of you for my dictionary.
|
|
|
|
|
I am very expensive. There is another guy who is better than me. Check my below post.
|
|
|
|