Wednesday, February 12, 2014

reverse of a string in c#.net

senario: 1


string res = "";
            string sa = "";
            string s = "hyd is good";

            for (int i = s.Length - 1; i = 0; i--)
            {

                res = res + s[i];
            }

Output:  "doog si dyh"







senario: 2







            string[] arr = s.Split(' ');

            for (int i = arr.Length - 1; i >= 0; i--)
            {
                sa += arr[i] + " ";
            }

Output:  "good ishyd"





senario: 3

  string[] arr = s.Split(' ');

            for (int i = arr.Length - 1; i >= 0; i--)
            {
                sa += arr[i] + " ";
            }

            for (int i = 0; i < arr.Length; i++)
            {
                string ss = arr[i];

                for (int j = ss.Length - 1; j >= 0; j--)
                {
                    res += ss[j];
                }
                res += " ";
            }

Output:  "dyh si doog"


 senario: 4


  public static string RecursivelyReverseString(string s)
        {
            if (s.Length > 0)
                return s[s.Length - 1] + RecursivelyReverseString(s.Substring(0, s.Length - 1));
            else
                return s;
        }

Output:  "doog si dyh"

No comments:

Post a Comment