Reputation:
I have a program see below
I made the method but I want to display it in the console and not on the easy way like console.writeline(str.length). I want using the method I made.
could someone help me please
thanks in advance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string str = "dit is een test 1,2,3";
Console.WriteLine(str);
}
public int CountAllNumbersAndChar(string str)
{
return str.Length;
}
}
}
I have the following program now
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string str = "this is a test 1,2,3";
int length = CountAllNumbersAndChar(str);
Console.WriteLine(str);
Console.WriteLine(length);// met de methode maar kan handiger met onderstaand voor beeld
// Console.WriteLine(str.Length);
// int numbers = str.Count(Char.IsNumber); // de makelijkste makelijke manier
//Console.WriteLine(numbers);
int countnumber = CountNumbers(str) ;
Console.WriteLine(countnumber);
int countwords = words(str);
Console.WriteLine(countwords);
}
public static int CountAllNumbersAndChar(string str)
{
return str.Length;
}
public static int CountNumbers(string str)
{
return str.Count(Char.IsNumber);
}
public static int words(string str)
{
int words = str.Split().Count(str => str.All(Char.IsLetter));
}
}
}
but it still doesnt work could someone say me what I have to change ?
Upvotes: 2
Views: 11274
Reputation: 14746
Here's how you do it. Note public
static
int CountAllNumbersAndChar(string str)
in the code below. You can't call CountAllNumbersAndChar
from Main
if you don't declare it as static
.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string str = "this is a test 1,2,3";
int length = CountAllNumbersAndChar(str);
Console.WriteLine(length);
}
public static int CountAllNumbersAndChar(string str)
{
return str.Length;
}
}
}
Upvotes: 2
Reputation: 7238
I think you want to count number of numbers inside your string
public int CountAllNumbersAndChar(string str)
{
return str.Split(new char[]{' ',','},
StringSplitOptions.RemoveEmptyEntries).Count
(
x=>
{
int d;
return int.TryParse(x,out d);
}
);
}
Upvotes: 0
Reputation: 2295
You could use LINQ for all these tasks. Although I'm not sure you are familiar with it. It's really simple though, so have a look at the code and see if you can follow.
string str = "dit is een test 1,2,3";
// Length of the string
int chars = str.Length;
// LINQ: Count all characters that is a number
int numbers = str.Count(Char.IsNumber);
// LINQ: Split the string on whitespace and count the
// elements that contains only letters
int words = str.Split().Count(s => s.All(Char.IsLetter));
Console.WriteLine(chars); // -> 21
Console.WriteLine(numbers); // -> 3
Console.WriteLine(words); // -> 4
Of course, the way I'm counting words there is not perfect, but it should get you started. For more accurate ways you should google it as there are hundreds of examples out there.
Upvotes: 0
Reputation: 49089
Is this what you want?
Console.WriteLine(CountAllNumbersAndChar(str));
Upvotes: 4