C# .NET - How can we reverse a string in C# without string Function?

Asked By Jai Ganesh
21-Jul-11 05:12 AM
Hi All,
 Can any one Tell me how to Reverse a String with out using string function() in C#?
For example i have string like
string s="He is palying in a ground.";
  Sreekumar P replied to Jai Ganesh
21-Jul-11 05:14 AM
HI,

public static string Reverse( string s )
{
  char[] charArray = s.ToCharArray();
  Array.Reverse( charArray );
  return new string( charArray );
}

I think the above works not tested, although the stringbuilder class may also have a reverse function I haven't checked that though.

  James H replied to Jai Ganesh
21-Jul-11 05:15 AM
REVERSE OF A STRING WITHOUT USING FUNCTION:

class ReverseString
   {
     public static void Main(string[] args)
     {
       string Name = "He is palying in a ground.";
       char[] characters = Name.ToCharArray();
       StringBuilder sb = new StringBuilder();
       for (int i = Name.Length - 1; i >= 0; --i)
       {
         sb.Append(characters[i]);
       }
       Console.Write(sb.ToString());
       Console.Read();
     }
   }

Hope this will help you :-)
  Riley K replied to Jai Ganesh
21-Jul-11 05:19 AM
Use this function

convert to character array and display from reverse by iterating reversely
public string Reverse(string text)
{
    char[] cArray = text.ToCharArray();
    string reverse = String.Empty;
    for (int i = cArray.Length - 1; i > -1; i--)
    {
        reverse += cArray[i];
    }
    return reverse;
}
  Reena Jain replied to Jai Ganesh
21-Jul-11 05:19 AM
hi,

Just pass your string in below function to get reverse order

public string Reverse(string str)
{
  int len = str.Length;
  char[] arr = new char[len];
 
  for (int i = 0; i < len; i++)
  {
  arr[i] = str[len - 1 - i];
  }
 
  return new string(arr);
}

hope this will help you
  Vickey F replied to Jai Ganesh
21-Jul-11 05:28 AM
Use this function



private string Reverse(string stringToReverse)
{ char[] rev = stringToReverse.Reverse().ToArray(); return new string(rev); }
  Jai Ganesh replied to James H
21-Jul-11 05:42 AM
Thanks Johnson.
  Jai Ganesh replied to Riley K
21-Jul-11 05:43 AM
Thanks Riley.
Create New Account
help
C# random alphanumeric strings ? .NET Framework Anyone got an example ? C# Discussions Console.WriteLine (1) StringBuilder (1) ToCharArray (1) NextDouble (1) Console (1) Random (1) Site (1) BlogMetaFinder (1) asdxcgfd - - Tom Spink University appropriate locking if you're going to use it from many threads) 3) Create a StringBuilder of the right length 4) In a for loop, append the next random character by in the string's length, and using string's indexer 5) Call ToString on the StringBuilder Jon Also, / / / using System; using System.Text; . . . public string GetRandomString (Random rnd, int length) { string charPool = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; StringBuilder rs = new StringBuilder(); while (length- - > 0) rs.Append(charPool[(int)(rnd.NextDouble() * charPool.Length)]); return rs.ToString(); } / / / Watch for wrapping on the charPool line! Note, the method takes an instance of the Random class, which you can instantiate with new Random(). The reason it doesn't do this itself is that (I think) the Random class uses a timer as it's entropy source, and if you try to generate a
the integer 687 Ouput 786 plz send the proper code HI PLEASE DO LIKE THIS class ReverseString { public static void Main( string [] args) { string Name = "I am God" ; char [] characters = Name.ToCharArray(); StringBuilder sb = new StringBuilder(); for ( int i = Name.Length - 1; i > = 0; - -i) { sb.Append(characters[i]); } Console.Write and display from reverse by iterating reversely public string Reverse(string text) { char[] cArray = text.ToCharArray(); string reverse = String.Empty; for (int i = cArray.Length - 1; i > -1; i- -) { reverse + = cArray this simple method to reverse string- public static string ReverseString (string s) { char[] arr = s.ToCharArray(); Array.Reverse(arr); return new string(arr); } Try and let me know. Array.Reverse Inverts Array Ordering string a = "I am God"; char[] arr = a.ToCharArray(); Array.Reverse(arr); string result = new string(arr); Create New Account
Generating the Random Key in c# Generating the Random Key in c# The RandomNumber class defined in the .NET Framework class library provides functionality to generate random key. Create the static class with name KeyGenerator. Declare the static readonly three char arrays. Define the static method GenerateRandomKey allow the numbers and allow the symbols. Generating the Random Key in c# The RandomNumber class defined in the .NET Framework class library provides functionality to generate random key. Create the static class with name KeyGenerator. Declare the static readonly three char arrays. Define the static method GenerateRandomKey KeyGenerator having the two overload methods. One takes no parameter and one has parameters. RandomNumber class is having the Next method. Next method takes maximum limit for the random key. Following random key. Enum CharacterType for random key. enum CharacterType { UpperCase, LowerCase, Number, Special } public static class KeyGenerator { private static readonly char [] _Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" .ToCharArray(); private static readonly char [] _Numbers = "1234567890" .ToCharArray
Collections.Generic; using System.Text; using System.Security.Cryptography; using System.Windows.Forms; namespace pass { class PasswordGenerator { public Form1 chk = new Form1(); public PasswordGenerator() { this.Minimum = DefaultMinimum; this.Maximum = DefaultMaximum; this randomCharPosition]; return randomChar; } } / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / public string Generate() / / all { int len2 = Int32.Parse(chk.numericUpDown2.Value.ToString()); StringBuilder pwdBuffer = new StringBuilder(); pwdBuffer.Capacity = this.Maximum; char lastCharacter, nextCharacter; lastCharacter = nextCharacter = ' \ n'; for (int i = 0; i ToString()); / / Pick random length between minimum and maximum / / int pwdLength = GetCryptographicRandomNumber(this.Minimum, this.Maximum); StringBuilder pwdBuffer = new StringBuilder(); pwdBuffer.Capacity = this.Maximum; / / Generate random characters char lastCharacter, nextCharacter; / / Initial dummy character flag lastCharacter return String.Empty; } } / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / public string Generate4() / / all { int len2 = Int32.Parse(chk.numericUpDown2.Value.ToString()); StringBuilder pwdBuffer = new StringBuilder(); pwdBuffer.Capacity = this.Maximum; char lastCharacter, nextCharacter; lastCharacter = nextCharacter = ' \ n'; for (int i = 0; i return String.Empty; } } / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / public string Generate5() / / all { int len2 = Int32.Parse(chk.numericUpDown2.Value.ToString()); StringBuilder pwdBuffer = new StringBuilder(); pwdBuffer.Capacity = this.Maximum; char lastCharacter, nextCharacter; lastCharacter = nextCharacter = ' \ n'; for (int