rani m  mk

rani m mk

  • NA
  • 50
  • 42.5k

Reverse only the word in the string not the whole string without using inbuilt functions

Jun 26 2012 7:03 AM
Hi all,

I have written the code below to reverse the words in the string or sentence.
Please run this in your system and check whether it is correct:

class Palindrome
    {
        public static void Main()
        {
            string text = "C Sharp Corner ";
            Console.WriteLine(PrintWordInReverse(text));

           
        }
        //Method to reverse the order
        static string PrintWordInReverse(string str)
        {
            string revWord = string.Empty;
            string finalsentence = string.Empty;
            for (int i = 0; i < str.Length; i++)
            {  if(str[i]!=null)
                {
                    revWord=str[i]+revWord;
                }
                else
                {
                    revWord=revWord+"";
                    finalsentence=finalsentence+revWord;
                    revWord=string.Empty;
                }
            }
            if (revWord != string.Empty)
            {
            }
             return finalsentence;
           
        }
    }

Answers (6)