22 August 2012

Get Middle of the Input String in C#.net

The following code will display the "Middle of the Input string using C#.net.
    public static string Midstring(string inputString)
    {
        int length = inputString.Length;
        Double   midvalue = length /2;
        if (length % 2 != 0)
        {
            int mid = Convert.ToInt32(midvalue);
            char[] InputString = inputString.ToCharArray();
            Reslt = InputString[mid].ToString();
        }
        else
        {
            Reslt = "Sorry, we can't give the Result";
        }
        return Reslt;
    }


Input: string Result = Midstring("Lakshmi");
MessageBox.Show("Result:" + " " + Result.ToUpper() + " " + " is a Middle of the Value");
Output: Result : S is a Middle of the Value.
In case Input Like this:
 Input: string Result = Midstring("Like");
MessageBox.Show("Result:" + " " + Result.ToUpper() + " " + " is a Middle of the Value");
 Output: Result : Sorry, we can't give the Result.

No comments: