Middle of the Input String in C#.net

Naraayanan
Posted by Naraayanan under C# category on | Points: 40 | Views : 2834
Hi,
The following code will display the "Middle of the Input string using C#.net.


#region 
public static string Reslt ;
#endregion

  #region Mid of the String
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;
}

#endregion Mid of the String



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.

Comments or Responses

Posted by: T.Saravanan on: 3/13/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Narayanan,

In your "MidString" method, you cannot declare variable "Reslt ". Kindly add these variable or change your code to


#region Mid of the String
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();
return InputString[mid].ToString();
}
else
{
return "Sorry, we can't give the Result";
}
}

#endregion Mid of the String

Posted by: Naraayanan on: 3/14/2012 Level:Starter | Status: [Member] | Points: 10
Hi, friend,
Thanks for reply .I updated my Code. Thanks

Login to post response