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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. reversing a string

reversing a string

Scheduled Pinned Locked Moved C#
data-structures
9 Posts 6 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.
  • R Offline
    R Offline
    Ravi Shankar43
    wrote on last edited by
    #1

    just i want the code for reversing a string through an array.

    J Q M G E 5 Replies Last reply
    0
    • R Ravi Shankar43

      just i want the code for reversing a string through an array.

      J Offline
      J Offline
      jdkulkarni
      wrote on last edited by
      #2

      Try doing this.

      Array.Sort(new string[] {"j", "a", "y", "a", "n", "t"});
      Array.Reverse();

      :)

      Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

      1 Reply Last reply
      0
      • R Ravi Shankar43

        just i want the code for reversing a string through an array.

        Q Offline
        Q Offline
        quiteSmart
        wrote on last edited by
        #3

        hi you can do this: String s = "the string to convert"; char [] array = s.ToCharArray(); char[] newArray; int count = 0; for (int i = array.Length - 1; i >= 0; i--) { newArray[count] = array[i]; count++; } String newString = new String(newArray); there should be more flexibale methods. but this came to my minde right now knowing that you have some conditions that it should be done using an array. hope it helps best regards

        G 1 Reply Last reply
        0
        • Q quiteSmart

          hi you can do this: String s = "the string to convert"; char [] array = s.ToCharArray(); char[] newArray; int count = 0; for (int i = array.Length - 1; i >= 0; i--) { newArray[count] = array[i]; count++; } String newString = new String(newArray); there should be more flexibale methods. but this came to my minde right now knowing that you have some conditions that it should be done using an array. hope it helps best regards

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          quiteSmart wrote:

          array[i] = newArray[count];

          Or perhaps the other direction... ;)

          --- b { font-weight: normal; }

          Q 1 Reply Last reply
          0
          • G Guffa

            quiteSmart wrote:

            array[i] = newArray[count];

            Or perhaps the other direction... ;)

            --- b { font-weight: normal; }

            Q Offline
            Q Offline
            quiteSmart
            wrote on last edited by
            #5

            Yea your right, a small mistake. i corrected it now thanks :laugh::)

            1 Reply Last reply
            0
            • R Ravi Shankar43

              just i want the code for reversing a string through an array.

              M Offline
              M Offline
              Mandaar Kulkarni
              wrote on last edited by
              #6

              Try this... string string1 = "String To Reverse"; string string2 = string.Empty; StringBuilder sb = new StringBuilder(); char[] sArr = string1.ToCharArray(); for (int j = sArr.Length - 1; j >= 0; j--) sb.Append(sArr[j].ToString()); string2 = sb.ToString();

              1 Reply Last reply
              0
              • R Ravi Shankar43

                just i want the code for reversing a string through an array.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                string message = "!dlrow olleH"; char[] temp = message.ToCharArray(); Array.Reverse(temp); message = new string(temp);

                --- b { font-weight: normal; }

                Q 1 Reply Last reply
                0
                • G Guffa

                  string message = "!dlrow olleH"; char[] temp = message.ToCharArray(); Array.Reverse(temp); message = new string(temp);

                  --- b { font-weight: normal; }

                  Q Offline
                  Q Offline
                  quiteSmart
                  wrote on last edited by
                  #8

                  nice and short good work:-D

                  1 Reply Last reply
                  0
                  • R Ravi Shankar43

                    just i want the code for reversing a string through an array.

                    E Offline
                    E Offline
                    ednrgc
                    wrote on last edited by
                    #9

                    If you want extra credit in the class, use recursion.

                    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