Character, Word and Line Count for Current File

, , , Thursday, March 19, 2009

// command wc(): Display on the message line,
// the total numbers of characters, words and
// lines
in the current file.
//
// This ubiquitous procedure
has been done ad
// infinitum by many developers. It's almost
// as common
as Hello World! I don't presume
// to hold any claim to it.
//
// On a large file of several thousand lines
// or more, it can take a few seconds to
// display its results

_command wc() {
cc = 0; // character count
wc = 0; // word count
lc = 0; // line count
item = 0;
_save_pos2(p);
top_of_buffer();
for (;;) {
get_line line;
lc++;
for (i = 1; i < length(line); i++) {
cc++;
if (isalnum(substr(line,i,1))) {
item = 1;
} else if (item && !isalnum(substr(line,i,1))) {
wc++;
item = 0;
}
}
status=down();
if (status == BOTTOM_OF_FILE_RC) {
message("chars: " cc " / words: " wc " / lines: " lc);
break;
}
}
_restore_pos2(p);
}

2 comments:

  1. Bernard said...

    Nice. I prefer the unix wc(1) order of lines / words / characters, for consistency.

  2. James Brown said...

    This content is written very well. Your use of when making your points makes your observations very clear and easy to understand. Thank you.
    character count tool

Post a Comment

GlossyBlue Blogger by Black Quanta. Theme & Icons by N.Design Studio
Entries RSS Comments RSS