Tuesday, January 27, 2009

function to build string Ex : Input 1a2b3c Output abbccc


private void btnTest_Click(object sender, EventArgs e)
{
MessageBox.Show(Build(textBox1.Text));
}


private String Build(String strInput)
{
String strOutput = "";
String tChar = "";
int nTimes = 0;
int nLen = 0 ,nPointer=0;
// strInput = "4a";
Char[] strChar = strInput.ToCharArray();
nLen = strChar.Length;
while(nPointer<nLen)
{
nTimes = 0;
bool bInt = true;
while (bInt && nPointer < nLen)
{
int res = 0;
bInt = Int32.TryParse(strChar[nPointer].ToString(),out res);
if(bInt)
{
nTimes = (nTimes * 10) + res;
nPointer++;
}
}
bInt = false;
tChar = "";
while (!bInt && nPointer < nLen)
{
int res = 0;
bInt = Int32.TryParse(strChar[nPointer].ToString(), out res);
if (!bInt)
{
tChar = tChar + strChar[nPointer];
nPointer++;
}
}
for (int n = 0; n < nTimes;n++ )
{
strOutput = strOutput + tChar;
}

}
return strOutput;
}

No comments:

Post a Comment