Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Efficient way to reverse the string inplace in c++??

Efficient way to reverse the string inplace in c++??

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Super Hornet

    Expecting favourable reply.

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #2

    Super Hornet wrote:

    Expecting favourable reply

    Explecting explicit and clear question, with code snippets and all that i need to understand the point with ambiguity...

    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    S 1 Reply Last reply
    0
    • T toxcct

      Super Hornet wrote:

      Expecting favourable reply

      Explecting explicit and clear question, with code snippets and all that i need to understand the point with ambiguity...

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      S Offline
      S Offline
      Super Hornet
      wrote on last edited by
      #3

      //function that retuens the reverse of passed string
      char *ReverseString(char *pszString)
      {
      if (pszString) //check for valid pointer
      {
      int ilength = strlen(pszString) + 1; //length of string including '\0'

      	for (int iIndex = ilength; iIndex > 0; iIndex--)
      	{
      		pszString\[i\] = pszString\[i-1\];  
      	}
      
      	pszString\[0\] = '\\0';
        
      	return &pszString\[ilength\];
      }
      else
      {
      	return NULL;
      }
      

      }

      //program to reverse the string inplace

      int main()
      {
      char szString[] = "hello world";

      //prints the string before calling ReverseString method
      cout << "String before reversing " << szString;
      
      szString = ReverseString(szStirng); //returns the reverse of a string
      
      //prints the string after calling ReverseString method
      cout << "\\n String after reversing " << szString;
      

      }

      Is the above code correct???

      T D 2 Replies Last reply
      0
      • S Super Hornet

        //function that retuens the reverse of passed string
        char *ReverseString(char *pszString)
        {
        if (pszString) //check for valid pointer
        {
        int ilength = strlen(pszString) + 1; //length of string including '\0'

        	for (int iIndex = ilength; iIndex > 0; iIndex--)
        	{
        		pszString\[i\] = pszString\[i-1\];  
        	}
        
        	pszString\[0\] = '\\0';
          
        	return &pszString\[ilength\];
        }
        else
        {
        	return NULL;
        }
        

        }

        //program to reverse the string inplace

        int main()
        {
        char szString[] = "hello world";

        //prints the string before calling ReverseString method
        cout << "String before reversing " << szString;
        
        szString = ReverseString(szStirng); //returns the reverse of a string
        
        //prints the string after calling ReverseString method
        cout << "\\n String after reversing " << szString;
        

        }

        Is the above code correct???

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #4

        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;
        

        }

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        modified on Wednesday, August 6, 2008 6:00 AM

        J N S 3 Replies Last reply
        0
        • T toxcct

          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;
          

          }

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          modified on Wednesday, August 6, 2008 6:00 AM

          J Offline
          J Offline
          Jijo Raj
          wrote on last edited by
          #5

          You can further optimize it by using std::reverse() algorithm...

          // Reverse it by using the Reverse alogrithm.
          string Str = _T("Hello");
          reverse( Str.begin(), Str.end());

          Regards, Jijo.

          _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

          T 1 Reply Last reply
          0
          • T toxcct

            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;
            

            }

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            modified on Wednesday, August 6, 2008 6:00 AM

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #6

            Dosen't string have a reserve() function? :confused:

            nave [OpenedFileFinder]

            T 1 Reply Last reply
            0
            • N Naveen

              Dosen't string have a reserve() function? :confused:

              nave [OpenedFileFinder]

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #7

              Naveen wrote:

              Dosen't string have a reserve() function?

              reserve() ? what for ?

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              N 1 Reply Last reply
              0
              • J Jijo Raj

                You can further optimize it by using std::reverse() algorithm...

                // Reverse it by using the Reverse alogrithm.
                string Str = _T("Hello");
                reverse( Str.begin(), Str.end());

                Regards, Jijo.

                _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #8

                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.

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                J 1 Reply Last reply
                0
                • T toxcct

                  Naveen wrote:

                  Dosen't string have a reserve() function?

                  reserve() ? what for ?

                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  N Offline
                  N Offline
                  Naveen
                  wrote on last edited by
                  #9

                  ops My bad. sorry I miss read the reserve as reverse.

                  nave [OpenedFileFinder]

                  1 Reply Last reply
                  0
                  • T toxcct

                    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.

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    J Offline
                    J Offline
                    Jijo Raj
                    wrote on last edited by
                    #10

                    :laugh: Regards, Jijo.

                    _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                    1 Reply Last reply
                    0
                    • T toxcct

                      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;
                      

                      }

                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                      modified on Wednesday, August 6, 2008 6:00 AM

                      S Offline
                      S Offline
                      Super Hornet
                      wrote on last edited by
                      #11

                      Thanks a-lot toxcct

                      1 Reply Last reply
                      0
                      • S Super Hornet

                        //function that retuens the reverse of passed string
                        char *ReverseString(char *pszString)
                        {
                        if (pszString) //check for valid pointer
                        {
                        int ilength = strlen(pszString) + 1; //length of string including '\0'

                        	for (int iIndex = ilength; iIndex > 0; iIndex--)
                        	{
                        		pszString\[i\] = pszString\[i-1\];  
                        	}
                        
                        	pszString\[0\] = '\\0';
                          
                        	return &pszString\[ilength\];
                        }
                        else
                        {
                        	return NULL;
                        }
                        

                        }

                        //program to reverse the string inplace

                        int main()
                        {
                        char szString[] = "hello world";

                        //prints the string before calling ReverseString method
                        cout << "String before reversing " << szString;
                        
                        szString = ReverseString(szStirng); //returns the reverse of a string
                        
                        //prints the string after calling ReverseString method
                        cout << "\\n String after reversing " << szString;
                        

                        }

                        Is the above code correct???

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #12

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups