Wednesday, May 16, 2012

How to Print Words in Reverse Order to the TextBox in asp.net


string strWord = "Welcome to my blog";
            string[] strArray = { "Welcome ", "to ", "my ", "blog " };
            string strOutput = string.Empty;
            for (int i = strArray.Length - 1; i <= strArray.Length; i--)
            {
                strOutput += strArray[i];
                if (i == 0)
                {
                    txtPName.Text = strOutput;
                    return;
                }
            }


  (or) 
protected void btnSave_Click(object sender, EventArgs e)
        {
            lblMsg.Text = PrintWordsInReverseOrder(
                txtEnterText.Text);

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


OutPut :
Input String :
 "Welcome to my blog"; 
Output String :
 "blog my to Welcome "; 
 

No comments:

Post a Comment

Comments

Search

Custom Search
Protected by Copyscape Plagiarism Software