12

I'd like to count the number of letters in a string variable.

I want to make a Hangman game. I need to know how many letters are needed to match the number of letters in the word.

7
  • 3
    You want to count letters, or words? Commented Jun 13, 2013 at 20:33
  • You should post any code you've tried. Commented Jun 13, 2013 at 20:33
  • 2
    What have you actually tried? Commented Jun 13, 2013 at 20:33
  • @crush Letters, sorry for the title. Commented Jun 13, 2013 at 20:34
  • 2
    Have you tried something along str.Count(char.IsLetter) ? Commented Jun 13, 2013 at 20:35

5 Answers 5

50
myString.Length; //will get you your result //alternatively, if you only want the count of letters: myString.Count(char.IsLetter); //however, if you want to display the words as ***_***** (where _ is a space) //you can also use this: //small note: that will fail with a repeated word, so check your repeats! myString.Split(' ').ToDictionary(n => n, n => n.Length); //or if you just want the strings and get the counts later: myString.Split(' '); //will not fail with repeats //and neither will this, which will also get you the counts: myString.Split(' ').Select(n => new KeyValuePair<string, int>(n, n.Length)); 
Sign up to request clarification or add additional context in comments.

9 Comments

@walther OP didn't suggest that as part of his problem or solution. Are we to assume what his intended result is?
@walther: In the game of hangman, you want to count the spaces.
@CL4PTR4P: In the game of hangman you don't want to count the spaces. You want to handle each word separately leaving a space between the words. So: "my hangman question" => "__ _______ ________"
@MattRazza It depends. Anyways, I'm adding a solution for that.
@MattRazza: But you still need to know they exist, and where.
|
4

You can simply use

int numberOfLetters = yourWord.Length; 

or to be cool and trendy, use LINQ like this :

int numberOfLetters = yourWord.ToCharArray().Count(); 

and if you hate both Properties and LINQ, you can go old school with a loop :

int numberOfLetters = 0; foreach (char letter in yourWord) { numberOfLetters++; } 

15 Comments

Why not yourWord.Length? Probably want to strip the spaces too. Of course, he'll probably want to put actual spaces where those spaces go.
@crush because it's LINQ. Length is old school
@IlyaIvanov But also more efficient...
@newStackExchangeInstance He was being ironic :)
guys, it's not only about performance, it's also about readability and intention-revealing code. Counting the length of a string with ToCharArray().Count() is like adding two numbers with Expression.Lambda<Func<int>>(Expression.Add(Expression.Constant(2), Expression.Constant(3))).Compile()() (it will return 5, just in case)
|
2

What is wrong with using string.Length?

// len will be 5 int len = "Hello".Length; 

3 Comments

@Leigh - thanks for the catch, edited answer and will remove comment.
string.length gives the string length in bytes not characters.
@Nate this is quite old, but pretty sure still holds true. Are you talking about another lang perhaps? Give it a shot and share result?
0

If you don't need the leading and trailing spaces :

str.Trim().Length 

4 Comments

That won't remove spaces...
str.Replace(" ", "").Length;
Also, Length is a property, not a method.
Isn't he counting the length of a single word? Well that's how I understood the question. My apologies.
-1
string yourWord = "Derp derp"; Console.WriteLine(new string(yourWord.Select(c => char.IsLetter(c) ? '_' : c).ToArray())); 

Yields:

____ ____

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.