How to find Mid Character for dynamic Input . For example i am giving "Apple" Means Display
Result: P.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Nov 23, 2013, 12:14 PM
In the case of input of even length(e.g. abcd), the mid character is taken as the first of the two middle characters (i.e. b):
Satyapriya NayakPosted Nov 23, 2013, 6:46 AM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Middle_chracter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string Result;
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();
Result = InputString[mid].ToString();
}
else
{
Result = "Sorry, we can't give the Result";
}
return Result;
}
private void btn_middle_char_Click(object sender, EventArgs e)
{
//string Result = Midstring(textBox1.Text);
string Result = Midstring("Apple");
MessageBox.Show("Result:" + " " + Result.ToUpper() + " " + " is middle character in the given string");
}
}
}