C
C#14mo ago
TheOne1984

✅ Trying to count numbers on a string

I want to count how many times any number appears in the string, in a way such that: "2" then numbers ++ and "351" then numbers++ every time any number appears, instead of counting the digits.
17 Replies
CoRoys
CoRoys14mo ago
Iterate over each character in a string, add to an integer if that character matches the specified number. I understood the first part of your question But could you elaborate on the rest?
TheOne1984
TheOne1984OP14mo ago
Yes, basically every time any number appears the counter goes +1 For instance +1 for 235
CoRoys
CoRoys14mo ago
So the amount of addition is not determined by the digit itself
TheOne1984
TheOne1984OP14mo ago
Instead of +3 for the digits Exactly, any number, if it has three, one or any digits it just adds +1
CoRoys
CoRoys14mo ago
What's separating these numbers in the string?
TheOne1984
TheOne1984OP14mo ago
spaces
CoRoys
CoRoys14mo ago
var numbers = str.Split(' ');
var counter = 0;

foreach(var num in numbers)
counter++;
var numbers = str.Split(' ');
var counter = 0;

foreach(var num in numbers)
counter++;
TheOne1984
TheOne1984OP14mo ago
Can you explain it to me?
CoRoys
CoRoys14mo ago
Pretty straightforward We're splitting the string with a whitespace splitter
TheOne1984
TheOne1984OP14mo ago
Oh I forgot, I cant use Split
CoRoys
CoRoys14mo ago
Why is that?
TheOne1984
TheOne1984OP14mo ago
My teacher wont allow functions he hasnt taught us
CoRoys
CoRoys14mo ago
Then create your own Split function
var numbers = new List<string>();
var tempStr = string.Empty;

foreach(var character in str)
{
if(character != ' ')
tempStr += character;
else
{
numbers.Add(tempStr);
tempStr = string.Empty;
}
}
var numbers = new List<string>();
var tempStr = string.Empty;

foreach(var character in str)
{
if(character != ' ')
tempStr += character;
else
{
numbers.Add(tempStr);
tempStr = string.Empty;
}
}
The fact I wrote this on my phone
TheOne1984
TheOne1984OP14mo ago
What is tempStr?
CoRoys
CoRoys14mo ago
A temporary string
TheOne1984
TheOne1984OP14mo ago
We havnt seen that either
CoRoys
CoRoys14mo ago
Dude it's just a string
Want results from more Discord servers?
Add your server