Search This Blog

Loading...

Dec 14, 2011

Tutorial on Asp.net: - How to print words in reverse order.


Sponsored Links:
You can print any string in reverse order by using below code, even no need to use any inbuild method of asp.net.


#Step 1. Create one method as 

PrintWordsInReverseOrder(string str)

static string PrintWordsInReverseOrder(string str)
    {
        string revWord = string.Empty;
        string finalSentance = string.Empty;
        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] != ' ')
            {
                revWord = str[i] + revWord;
            }
            else
            {
                revWord = revWord + ' ';
                finalSentance = finalSentance + revWord;
                revWord = string.Empty;
            }
        }
        if (revWord != string.Empty)
            finalSentance = finalSentance + revWord;
        return finalSentance;
    }


Now call this method any where, i am calling in page_load event as

protected void Page_Load(object sender, EventArgs e)
   {
       Response.Write(PrintWordsInReverseOrder("Test String"));   
   }

Follow by Email