hi,
iam using below code for my below task
| task-1 | task-2 | |
| textbox1 | textbox1 | |
| 1.2.3. | [1.2.3. | |
| 4.5.6. | 4.5.6.]10. | |
| 7.8.9. | 7.8.9. | |
| 10.11.12. | 10.11.12. | |
| [1.2. | ||
| 3.4.]2. | ||
| 1.2.3.4 | ||
| 4 = lines - 12=no | 27 = lines - 78=no |
C#
private void RsButton7_Click_1(object sender, EventArgs e)
{
int countLines = 0;
int countNumbers = 0;
var stack = new Stack();
int num;
foreach (string line in txtDraft.Lines)
{
if (line.Trim() == "") continue; // ignore blank lines
string[] items = line.Split('.');
foreach (string item in items)
{
if (int.TryParse(item, out num)) countNumbers++;
}
countLines++;
rsLabel1.Text = countLines + " = lines" + " - " + countNumbers + " = no".ToString();
}
}task-1 work well using above code.
but i want also like task-2 like above given task-2 input.
please anybody share the update code.
Thanks in Advance.
Varta

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Nov 6, 2024, 2:39 AM EST
Hi Luv,
Here is the revised code which you can use.
private void button2_Click(object sender, EventArgs e) { int countLines = 0; int countNumbers = 0; bool insideBrackets = false; int bracketNumbersCount = 0; int bracketLinesCount = 0; // To count lines within a bracket section int multiplier = 1; foreach (string line in txtDraft.Lines) { // Skip blank lines or lines with only whitespace if (string.IsNullOrWhiteSpace(line)) continue; if (line.Contains("[")) { insideBrackets = true; bracketNumbersCount = 1; // Reset count for new bracketed section bracketLinesCount = 1; // Start counting lines within brackets } if (insideBrackets) { // Count numbers inside the brackets string content = line; if (line.Contains("]")) { // End of bracketed section insideBrackets = false; content = content.Substring(0, content.IndexOf(']')).Trim(); // Extract multiplier if it exists after the bracket string remainingText = line.Substring(line.IndexOf(']') + 1).Trim(); if (int.TryParse(remainingText.Split('.')[0], out int result)) { multiplier = result; } else { multiplier = 1; // Default multiplier if not specified } } // Split and count numbers within this part of the bracketed content string[] items = content.Split('.'); foreach (string item in items) { if (int.TryParse(item.Trim(), out _)) { bracketNumbersCount++; } } // If we're at the end of the brackets, apply the multiplier if (!insideBrackets) { countNumbers += bracketNumbersCount * multiplier; countLines += bracketLinesCount * multiplier; // Apply line multiplier for bracketed section bracketNumbersCount = 1; // Reset for the next bracketed section bracketLinesCount = 0; multiplier = 1; // Reset multiplier for next section } else { bracketLinesCount++; // Count lines within bracket } } else { // Count numbers and lines outside of brackets normally string[] items = line.Split('.'); foreach (string item in items) { if (int.TryParse(item.Trim(), out _)) { countNumbers++; } } countLines++; } } rsLabel1.Text = $"{countLines} = lines - {countNumbers} = numbers"; }Thank you.
Local Coders197 reputationPosted Nov 2, 2024, 12:41 PM EDT
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Count lines
int lineCount = textBox1.Lines.Length;
labelLineCount.Text = "Lines: " + lineCount;
// Count numbers
int numberCount = CountNumbers(textBox1.Text);
labelNumberCount.Text = "Numbers: " + numberCount;
}
private int CountNumbers(string text)
{
int numberCount = 0;
string[] words = text.Split(new char[] { ' ', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in words)
{
if (int.TryParse(word, out _)) // Checks if the word is a number
{
numberCount++;
}
}
return numberCount;
}
Luv LRank #8,316 · 224 reputationPosted Nov 2, 2024, 1:35 PM EDT
Dear @Local Coders,
Thanks for your reply.i have already done result same as above code for task-1 like above mentioned.
if i want to start "[" open and "]"close bracket with multiply tnumbers like 10 (multiply number may be any number) used like above task2.then result will come as per above code.if i want to change the input in textbox using bracket with timing what is the impliment of code.
i hope ypu understand my logic.
Thanks.
Luv LRank #8,316 · 224 reputationPosted Nov 4, 2024, 2:21 AM EST
Kindly anybody solve this.
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Nov 4, 2024, 6:12 AM EST
Hi Luv L,
You can try the below code for your problem statement.
private void RsButton7_Click_1(object sender, EventArgs e) { int countLines = 0; int countNumbers = 0; bool insideBrackets = false; int bracketNumbersCount = 0; int multiplier = 1; foreach (string line in txtDraft.Lines) { // Skip blank lines or lines with only whitespace if (string.IsNullOrWhiteSpace(line)) continue; if (line.Contains("[")) { insideBrackets = true; bracketNumbersCount = 1; // Start counting numbers inside brackets } if (insideBrackets) { // Count numbers inside the brackets string content = line; if (line.Contains("]")) { // End of bracketed section insideBrackets = false; content = content.Substring(0, content.IndexOf(']')).Trim(); // Extract multiplier if it exists after the bracket string remainingText = line.Substring(line.IndexOf(']') + 1).Trim(); if (int.TryParse(remainingText.Split('.')[0], out int result)) { multiplier = result; } else { multiplier = 1; // Default multiplier if not specified } } // Split and count numbers within this part of the bracketed content string[] items = content.Split('.'); foreach (string item in items) { if (int.TryParse(item.Trim(), out _)) { bracketNumbersCount++; } } // If we're at the end of the brackets, apply the multiplier if (!insideBrackets) { countNumbers += bracketNumbersCount * multiplier; multiplier = 1; // Reset multiplier for next section } } else { // Count numbers outside of brackets normally string[] items = line.Split('.'); foreach (string item in items) { if (int.TryParse(item.Trim(), out _)) { countNumbers++; } } } countLines++; } rsLabel1.Text = $"{countLines} = lines - {countNumbers} = numbers"; }Thank you.!
Luv LRank #8,316 · 224 reputationPosted Nov 5, 2024, 12:43 AM EST
@Muhammad Imran Ansari,
Thanks for code.in this code count numbers work ok.but count lines not work correct.if we use bracket with then its not caluclated lines.
Textbox1
1.2.3.
4.5.6.
[1.2.
3.4]5.
1.2.3.4.
5 = lines
30 = numbers
Here 5 lines is wrong.
it should be after close bracket number is 5
so 5 x 2 lines = 10. + remain without using bracket
lines 3
so taltal lines should be 13 is correct answer.
so look in to this and share revised pls.