5
\$\begingroup\$

Counting characters of a program manually is tedious, and we do this a lot in Code Golf. How do you do a more effective character count using utilities provided by your shell or ide?

asked Jan 27, 2011 at 22:18
\$\endgroup\$
11
  • \$\begingroup\$ (I know the answer, but wanted to try such a question to see if we consider this on-topic.) \$\endgroup\$ Commented Jan 27, 2011 at 22:19
  • \$\begingroup\$ Additionally should we include the counts of special compiler/interpreter flags in the character counts? \$\endgroup\$ Commented Jan 27, 2011 at 22:22
  • \$\begingroup\$ @JPvdMerwe That's a good question, but ask that on meta where we're fleshing out the description and usage of this site. \$\endgroup\$ Commented Jan 27, 2011 at 22:25
  • 1
    \$\begingroup\$ @Close voters: Since we're defining this site, can you please give reasons as to why you think should be off-topic. This post was experimental in that regard, but no feedback makes it a pointless exercise. \$\endgroup\$ Commented Jan 27, 2011 at 22:42
  • \$\begingroup\$ It's either meta.codegolf.stackexchange.com or superuser.com, because there is no puzzle. \$\endgroup\$ Commented Jan 27, 2011 at 23:16
  • 1
    \$\begingroup\$ @Nakilon The area51 proposal had at least one meta-ish on-topic question: "Is code golfing the practice of elegance or obfuscation?" I disagree that this is for SU, as we've seen in answers there are specific details around newlines (and probably unicode too). \$\endgroup\$ Commented Jan 27, 2011 at 23:21
  • \$\begingroup\$ @Nakilon See meta.codegolf.stackexchange.com/questions/27/… \$\endgroup\$ Commented Jan 27, 2011 at 23:25
  • \$\begingroup\$ @marcog, if you asked this (about counting lines) question at proposal stage, I think, we could see mostly off-topic votes. \$\endgroup\$ Commented Jan 27, 2011 at 23:46
  • \$\begingroup\$ @Nakilon Unfortunately I missed the definition stage, which is why I'm asking it now. \$\endgroup\$ Commented Jan 27, 2011 at 23:49
  • \$\begingroup\$ @Nakilon: The question is whether meta is "meta-puzzle/golf" or "meta-this-site" or both. I saw it more as "meta-this-site" so far and "meta-golf" questions would then be on scope. Just my opinion, though. \$\endgroup\$ Commented Jan 31, 2011 at 12:40
  • \$\begingroup\$ @Joey Same opinion I voiced here. \$\endgroup\$ Commented Jan 31, 2011 at 13:00

10 Answers 10

4
\$\begingroup\$

On Linux, use wc -c file in your favourite shell. Note that you might be counting a trailing new-line, which you might want to ignore.

answered Jan 27, 2011 at 22:20
\$\endgroup\$
2
  • 4
    \$\begingroup\$ Any unix, not just linux. And you can ignore the -c if you're going to parse the output by Mk. I eyeball. \$\endgroup\$ Commented Jan 27, 2011 at 22:33
  • \$\begingroup\$ ‐c counts bytes. This stems from the sometimes erroneous historical view that bytes and characters are the same size. (source: POSIXTM) This question, however, is asking for character count. The flag for that is ‐m: wc ‐m file... is the way to go. \$\endgroup\$ Commented Sep 5, 2023 at 10:17
2
\$\begingroup\$

Notepad++ includes a convenient count of both characters (not including newlines) and bytes (be sure to first remove any trailing newlines and switch to Unix newlines) at the bottom of the window. Separate counts exist for the current selection and the entire file.

answered Jan 31, 2011 at 12:13
\$\endgroup\$
1
  • \$\begingroup\$ That's probably what I'll use for most of my solutions. If I ever do one, that is. \$\endgroup\$ Commented Feb 2, 2011 at 4:42
1
\$\begingroup\$
answered Jan 27, 2011 at 22:40
\$\endgroup\$
1
\$\begingroup\$

Usually I use one of these two ways:

  1. I always have my Total Commander (FAR, MC, what do you wish) opened at current working directory, so I just look on file size in bytes and substract (number_of_lines - 1) if I used CR LF;
  2. If I'm too lazy to open Total, I go to IRC and give solution code to bot to count it's length.
answered Jan 27, 2011 at 23:13
\$\endgroup\$
1
  • \$\begingroup\$ Shift+F2, U, Enter in Far to save it with LF line endings. :-) \$\endgroup\$ Commented Jan 31, 2011 at 12:31
1
\$\begingroup\$

I just look at the character count when I open it in vim to copy across.

answered Jan 28, 2011 at 5:25
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Vim appends one unnecessary new line at the end of file. \$\endgroup\$ Commented Jan 28, 2011 at 21:30
  • \$\begingroup\$ @Alexandru: :set binary noeol \$\endgroup\$ Commented Feb 4, 2011 at 22:14
1
\$\begingroup\$

For most golfings I just use Far, i.e. an orthodox file manager, save the file with LF line endings and just look at the file size (I need to remember svn ps svn:eolstyle LF * though because SVN tends to think too much). Since I'm on Windows there's luckily no trailing LF to be accounted for.

However, for one-liners in PowerShell, I have a small utility (slightly buggy, but never got around fixing them):

function Set-GolfPrompt {
 function global:prompt {
 # Info about how much shorter/longer the previous command was can only
 # be given if the history is long enough, so don't do that if not.
 $history = Get-History
 if ($history.Length -ge 2) {
 $last = (Get-History)[-1].CommandLine.Length
 2ドルndlast = (Get-History)[-2].CommandLine.Length
 Write-Host -NoNewline -ForegroundColor DarkGray "Last command's length: $last "
 if (2ドルndlast -eq $last) {
 Write-Host -NoNewline -ForegroundColor Gray "(?0)"
 } elseif (2ドルndlast -lt $last) {
 Write-Host -NoNewline -ForegroundColor Red "(+$($last - 2ドルndlast))"
 } else {
 Write-Host -NoNewline -ForegroundColor Green "(-$(2ドルndlast - $last))"
 }
 }
 [Console]::Write("`n ")
 0..10 |
 ForEach-Object {
 [Console]::ForegroundColor = ($_+1) % 2 * 2 + 12
 [Console]::Write("$_ ")
 }
 [Console]::Write("`n ")
 1..11 |
 ForEach-Object {
 [Console]::ForegroundColor = $_ % 2 * 2 + 12
 [Console]::Write('1234567890')
 }
 [Console]::ForegroundColor = 'White'
 [Console]::WriteLine()
 "> "
 }

Which then looks like this:

enter image description here

answered Jan 31, 2011 at 12:37
\$\endgroup\$
1
\$\begingroup\$

I like to paste the code into charactercountonline.com . I especially enjoy that I don't need to include the whole file in the count, because I often include useless information that doesn't need to be counted (e.g. in Java: package codegolf;).

answered Jun 17, 2014 at 21:03
\$\endgroup\$
1
\$\begingroup\$

I usually use Sublime for counting (even if I'm not writing the code in it). I paste the code I want to count into a new file. Then, if I'm sure that my code still works without any whitespace whatsoever, I prepare it with

  • Ctrl +H (open Find & Replace)
  • (optionally) Alt + R (activate regex search)
  • \ s (search for whitespace)
  • (optionally) Tab Del (clear replacement string)
  • Ctrl + Alt + Enter (replace all)
  • Ctrl + A (select all)

Now in the status bar at the bottom you'll see "x characters selected". This sequence of key presses becomes really mechanical after a while, and when you're doing it repeatedly, because you've been golfing more in the meantime, you can actually skip steps 2 to 4. Of course, some of those combinations may be configured differently on your machine, but I think they are the default key bindings in Windows.

This is particularly useful for Mathematica answers, because if you paste stuff out of Mathematica, you get tons of unwanted whitespace.

answered Jun 18, 2014 at 9:45
\$\endgroup\$
3
  • \$\begingroup\$ And how do you count bytes for Mma programs? \$\endgroup\$ Commented Oct 10, 2014 at 3:57
  • \$\begingroup\$ @belisarius If I've got Unicode (in any language), after the above steps, I'll paste it into this online byte counter. Of course, in Mathematica in particular, you sometimes need to insert the Unicode characters by hand, because when copying out of Mathematica, it will replace stuff like with \[Intersection]. \$\endgroup\$ Commented Oct 10, 2014 at 8:42
  • \$\begingroup\$ Yeah, thanks a lot. That char replacement is what sometimes drives me nuts. BTW, I count chars with Sublime too :) \$\endgroup\$ Commented Oct 10, 2014 at 8:59
0
\$\begingroup\$
$ python
>>> len("".join(open('filename').readlines()))

or, as hallvabo suggests:

$ python
>>> len(open('filename').read())
answered Jan 31, 2011 at 9:55
\$\endgroup\$
4
  • \$\begingroup\$ This would count the number of lines, not characters. \$\endgroup\$ Commented Jan 31, 2011 at 21:50
  • \$\begingroup\$ @hallvabo, Good point. I've fixed it. \$\endgroup\$ Commented Feb 1, 2011 at 10:15
  • \$\begingroup\$ Why not just open('filename').read()? And to avoid any behind-the-scenes automagial line terminator translation, use 'rb' as mode flag. \$\endgroup\$ Commented Feb 1, 2011 at 12:44
  • \$\begingroup\$ @hallvabo, I didn't know you could do that. \$\endgroup\$ Commented Feb 1, 2011 at 12:45
0
\$\begingroup\$

Usually I am using vim, so for one liners I position the cursor on the last character and read off that position

For multiple lines, I save the file and subtract 1 from the reported size (because of the extra newline)

If I solve a problem in one line in the python interpreter, I'll just paste it into

>>> len("""<solution>""")

(I don't think I've ever had an answer with """ in it :))

answered Feb 2, 2011 at 21:03
\$\endgroup\$
1
  • 1
    \$\begingroup\$ on vim: g <Ctrl>-g \$\endgroup\$ Commented Feb 4, 2011 at 22:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.