C
C#8mo 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
CrumpetMan
CrumpetMan8mo 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
TheOne19848mo ago
Yes, basically every time any number appears the counter goes +1 For instance +1 for 235
CrumpetMan
CrumpetMan8mo ago
So the amount of addition is not determined by the digit itself
TheOne1984
TheOne19848mo ago
Instead of +3 for the digits Exactly, any number, if it has three, one or any digits it just adds +1
CrumpetMan
CrumpetMan8mo ago
What's separating these numbers in the string?
TheOne1984
TheOne19848mo ago
spaces
CrumpetMan
CrumpetMan8mo 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
TheOne19848mo ago
Can you explain it to me?
CrumpetMan
CrumpetMan8mo ago
Pretty straightforward We're splitting the string with a whitespace splitter
TheOne1984
TheOne19848mo ago
Oh I forgot, I cant use Split
CrumpetMan
CrumpetMan8mo ago
Why is that?
TheOne1984
TheOne19848mo ago
My teacher wont allow functions he hasnt taught us
CrumpetMan
CrumpetMan8mo 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
TheOne19848mo ago
What is tempStr?
CrumpetMan
CrumpetMan8mo ago
A temporary string
TheOne1984
TheOne19848mo ago
We havnt seen that either
CrumpetMan
CrumpetMan8mo ago
Dude it's just a string