Reputation: 33
I am trying to write a code in C# that counts how many numbers are there in a sentence. So if the number is made up of more than a digit it should be counted as one number. For example: 2021 is one number not 4 numbers
This problem is caused by the method 'Char.IsDigit'
I wrote a code but the only problem in my algorithms is that if a word starts with a number it will be counted as a number. For example: 1big will be counted as a number, but it shouldn't.
So how can I solve this problem?
My code might be a trivial code, so if there are better solutions I would be happy if you show me.
using System;
namespace Textanalyse
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("I count how many numbers are there in a text.");
string text = "Hallo to the 1 that broke all my 12345 bones in my 2 legs, 2 hands and 20 fingers without touching my skull that has an IQ100 brain but 1big bone";
Console.WriteLine($"There are {countTheNumbers(text)} in the sentence above");
Console.ReadKey();
}
public static int countTheNumbers(string s)
{
int sum = 0;
string[] everyWord = s.Split(" ");
bool x;
for (int i = 0; i < everyWord.Length; i++)
{
Console.WriteLine($"The {i + 1}. String is \"{everyWord[i]}\" and it contains ({everyWord[i].Length}) digits.");
for (int j = 0; j < everyWord[i].Length; j++)
{
x = char.IsDigit(everyWord[i], j);
Console.WriteLine(x);
if (x == false)
break;
else
{
sum = sum + 1;
Console.WriteLine($"\"{everyWord[i]}\" is a number");
break;
}
}
}
return sum;
}
}
}
Upvotes: 0
Views: 620
Reputation: 33
So thank u everyone for everything I really learned a lot from you
I found another idea that would work with my prof. thanks
using System;
namespace Textanalyse
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("I count how many numbers are there in a string.\nPlease enter a text: ");
string satz = Console.ReadLine();
Console.WriteLine($"There are {ZaehleZahlen(satz)} numbers in this sentence! ");
Console.ReadKey();
}
public static int ZaehleZahlen(string s)
{
int sum = 0;
string[] jedesWort = s.Split(" ");
for (int i = 0; i < jedesWort.Length; i++)
{
for (int j = 0; j < jedesWort[i].Length; j++)
{
bool x;
x = char.IsDigit(jedesWort[i], j);
if (x == false)
break;
else if (x == true)
{
if (j != jedesWort[i].Length-1) continue;
else
{
sum = sum + 1;
Console.WriteLine($"\"{jedesWort[i]}\" is a number");
}
}
}
}
return sum;
}
Upvotes: 0
Reputation: 186688
Well, if we define "number" as sequence of digits '0'..'9'
with word borders we can use regular expressions, e.g. having
string text =
"Hallo to the 1 that broke all my 12345 bones in my 2 legs, 2 hands and 20 fingers without touching my skull that has an IQ100 brain but 1big bone";
We can put
using System.Text.RegularExpressions;
...
int count = Regex.Matches(text, @"\b[0-9]+\b").Count;
Here
\b - word border
[0-9]+ - one or more digits in '0'..'9' range
\b - word border
Let's have a look at these numbers:
var result = Regex
.Matches(text, @"\b[0-9]+\b")
.Cast<Match>()
.Select(m => m.Value);
string report = string.Join(Environment.NewLine, result);
Console.Write(report);
Outcome:
1
12345
2
2
20
Upvotes: 2
Reputation: 4440
This is a working solution for you case with comments explaining what each line does so you can understand and learn. there are many more that you can do to it to make it better and fool proof but for your current test case it's enough
string text = "Hallo to the 1 that broke all my 12345 bones in my 2 legs, 2 hands and 20 fingers without touching my skull that has an IQ100 brain but 1big bone";
// split the text by spaces so we end up with a collection of words
int count = text.Split(new[] { ' ' })
// test all words in the collection
.Where(word =>
// check if all element of the word matches a condition
// a string in composed of char so it will iterate on each char
word.All(letter =>
// return true if the character is a digit
char.IsDigit(letter)))
// return the count of all words we just filtered and kept if they were all made of digits
.Count();
Upvotes: 2