Reverse the stirng without using any inbuilt function in C#.

Input: I am John

output: I ma nhoJ

public static void ReverseString(string str)
        {
           string strNew = string.Empty;
            int length = str.Length;
            string temp = string.Empty;
            string[] arr = new string[10];
            char[] charar = str.ToCharArray();
            char chTemp;
            for (int i = 0; i < length / 2; ++i)
            {
                chTemp = charar[length – 1 – i];
                charar[length – 1 – i] = charar[i];
                charar[i] = chTemp;
            }
            for (int i = 0, j = 0; i < length; ++i)
            {
                if (charar[i].ToString() == ” “)
                {
                    arr[j++] = temp + ” “;
                    temp = string.Empty;
                }
                else
                {
                    temp += charar[i];
                    if (i == length – 1)
                    {
                        arr[j++] = temp + ” “;
                        temp = string.Empty;
                    }
                }
            }
            for (int i = arr.Length – 1; i >= 0; –i)
            {
                strNew += arr[i];
            }
            Console.WriteLine(strNew);
            Console.ReadLine();
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s