C#

Reverse word in a string


Reverse word in a string without using inbuilt function in C#

reverse word in string


class ResverseWord
    {
        public static void Main()
        {
            string s = "Hi , how are you ?";

            //use for store string into list of word
            List<string> lst = new List<string>();

            //use for populate reverse word string
            string finalString = string.Empty;

            string str = string.Empty;

            for (int i=0;i<s.Length;i++)
            {
                if(s[i]==' ')
                {
                  
                    lst.Add(str);
                    str = "";
                }
                else
                {
                    str += s[i];
                }
                if(i==s.Length-1)
                    lst.Add(str);
            }
            for(int i=lst.Count-1;i>=0;i--)
            {
                finalString += lst[i]+" ";
            }

            Console.WriteLine(finalString);

            Console.ReadLine();
        }
    }

1 comment: