views:

381

answers:

6

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; 
        }

    }
}

Update:

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 ?

+4  A: 

Is this what you want?

Console.WriteLine(CountAllNumbersAndChar(str));
Artelius
+2  A: 

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; 
        }
    }
}
Joren
A: 

What did you want?

Macroideal
Don't post any answers unless they you know *contain an answer*
Nifle
A: 

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.

Manticore
no extension method Count for string
Ahmed Said
Have you included using for System.Linq?
Manticore
yes, I think so
Ahmed Said
you can check my answer
Ahmed Said
Be sure to reference System.Core too. The code is really working, I don't know why you are saying otherwise.
Manticore
I found the problem, but it is not related to any assembly, the auto complete does not display the extension method count for the string, but when I write it, it displays tool tip!!! strange
Ahmed Said
but again your code, count numbers incorrectly as numbers larger than 9 will cause a problem
Ahmed Said
A: 

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);
           }
        );   
       }
Ahmed Said
How is this better than `str.Count(Char.IsNumber)`? Also, please format your code to make it more readable.
Manticore
str.Count will iterate on each character, for example 19 will be counted as 2 !!!
Ahmed Said
That is true. In that case you'd have to use something like your example, indeed.
Manticore
A: 

i mean how can i count all words ! sorry for the confusion of me. i have put it in a method but i get the error that not all code paths return a value

public static int words(string str) { return str.Split().Count(s => s.All(Char.IsLetter));

that is what i write in the methode

and this is how i wanted to display it

 int woorden = words;
        Console.WriteLine(woorden);
`words` is a method, you need to send a string to it or it will fail. I.e. `words("Hello everyone");`
Manticore