Given a string may contain spaces, convert them to
- All first character caps
- All Even numbers caps
- All Odd Numbers small
- Consider the strings are starting with 0 - Even number
Other conditions:
The given string is non null
HTML
<br />
<br />
<div>
<p>
Coding Challenge : Write an ASP.NET program to acheive the following!
</p>
<table style="width: 100%; border: dotted;" border="1">
<tr>
<td>
<p>
Given a string may contain spaces, convert them to
</p>
<ul>
<li>All first character caps</li>
<li>All Even numbers caps</li>
<li>All Odd Numbers small</li>
<li>Consider the strings are starting with 0 - Even number</li>
</ul>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtInput" Width="100%" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="btnSubmit" runat="server" Text="Process" OnClick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
C# Program
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblResult.Text = ProcessString(txtInput.Text);
}
public static string ProcessString(string s)
{
List<string> LstResult = new List<string>();
Func<char, bool, string> doProc = (chr, isTrue) =>
{
if (isTrue) return chr.ToString().ToUpper();
else return chr.ToString().ToLower();
};
string[] StrArr = s.Split(' ').ToArray();
foreach (var singleString in StrArr)
{
string returnVal = string.Empty;
for (int i = 0; i < singleString.Length; i++)
{
if (i == 0)
{
returnVal+=doProc(singleString[i], true);
}
else if (i == 1)
{
returnVal += doProc(singleString[i], false);
}
else if (i % 2 == 0)
{
returnVal += doProc(singleString[i], true);
}
else
{
returnVal += doProc(singleString[i], false);
}
}
LstResult.Add(returnVal);
}
return string.Join(" ", LstResult);
}
Comments