Click here to Skip to main content
15,914,488 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How can Create Shortcut key? Pin
Le@rner6-Aug-08 18:21
Le@rner6-Aug-08 18:21 
AnswerRe: How can Create Shortcut key? Pin
Perspx6-Aug-08 13:35
Perspx6-Aug-08 13:35 
GeneralRe: How can Create Shortcut key? Pin
Le@rner6-Aug-08 18:45
Le@rner6-Aug-08 18:45 
GeneralRe: How can Create Shortcut key? Pin
Perspx6-Aug-08 23:04
Perspx6-Aug-08 23:04 
QuestionEfficient way to reverse the string inplace in c++?? Pin
Super Hornet5-Aug-08 22:05
Super Hornet5-Aug-08 22:05 
AnswerRe: Efficient way to reverse the string inplace in c++?? Pin
toxcct5-Aug-08 22:25
toxcct5-Aug-08 22:25 
GeneralRe: Efficient way to reverse the string inplace in c++?? Pin
Super Hornet5-Aug-08 22:49
Super Hornet5-Aug-08 22:49 
GeneralRe: Efficient way to reverse the string inplace in c++?? [modified] Pin
toxcct5-Aug-08 23:02
toxcct5-Aug-08 23:02 
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

GeneralRe: Efficient way to reverse the string inplace in c++?? Pin
Jijo.Raj5-Aug-08 23:15
Jijo.Raj5-Aug-08 23:15 
GeneralRe: Efficient way to reverse the string inplace in c++?? Pin
toxcct5-Aug-08 23:17
toxcct5-Aug-08 23:17 
JokeRe: Efficient way to reverse the string inplace in c++?? Pin
Jijo.Raj5-Aug-08 23:21
Jijo.Raj5-Aug-08 23:21 
GeneralRe: Efficient way to reverse the string inplace in c++?? Pin
Naveen5-Aug-08 23:16
Naveen5-Aug-08 23:16 
GeneralRe: Efficient way to reverse the string inplace in c++?? Pin
toxcct5-Aug-08 23:17
toxcct5-Aug-08 23:17 
GeneralRe: Efficient way to reverse the string inplace in c++?? Pin
Naveen5-Aug-08 23:20
Naveen5-Aug-08 23:20 
GeneralRe: Efficient way to reverse the string inplace in c++?? Pin
Super Hornet6-Aug-08 3:42
Super Hornet6-Aug-08 3:42 
GeneralRe: Efficient way to reverse the string inplace in c++?? Pin
David Crow6-Aug-08 7:08
David Crow6-Aug-08 7:08 
QuestionCListCtrl Pin
john56325-Aug-08 21:43
john56325-Aug-08 21:43 
AnswerRe: CListCtrl Pin
Hamid_RT5-Aug-08 22:02
Hamid_RT5-Aug-08 22:02 
AnswerRe: CListCtrl Pin
_AnsHUMAN_ 5-Aug-08 22:04
_AnsHUMAN_ 5-Aug-08 22:04 
QuestionChecking internet connection is present or not... Pin
VCProgrammer5-Aug-08 21:42
VCProgrammer5-Aug-08 21:42 
AnswerRe: Checking internet connection is present or not... Pin
Jijo.Raj5-Aug-08 22:02
Jijo.Raj5-Aug-08 22:02 
GeneralRe: Checking internet connection is present or not... Pin
VCProgrammer5-Aug-08 22:16
VCProgrammer5-Aug-08 22:16 
GeneralRe: Checking internet connection is present or not... Pin
Jijo.Raj5-Aug-08 22:23
Jijo.Raj5-Aug-08 22:23 
GeneralRe: Checking internet connection is present or not... Pin
VCProgrammer5-Aug-08 23:12
VCProgrammer5-Aug-08 23:12 
AnswerRe: Checking internet connection is present or not... Pin
_AnsHUMAN_ 5-Aug-08 23:18
_AnsHUMAN_ 5-Aug-08 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.