473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

working word counter

After getting some help working through my bugs, I have what seems to
be a robust, working word counter script. I post it here to benefit
others that might want this in the future and so that if I ever lose
my copy I can come back here to find it :) Some other scripts that I
used for inspiration failed when confronted with whitespace before the
string or miscalculated when encountering linefeeds and other
non-space spaces, so I made mine better. Definition of words for this
exercise is contiguous groups of characters separated by whitespace.
Maybe it will even be useful to somebody besides me.

Cheers!
--David

<html>
<!--Generated 02/12/04-->
<!--modified 02/15/04-->
<!--name of this file: "wordCounter.html" -->

<head>
<title>Word Counter</title>
<SCRIPT LANGUAGE="JavaScript">

function trim(data)
{
while (/\s/.test(data.charAt(0)))
{data=data.substring(1,data.length);}
while (/\s/.test(data.charAt(data.length-1)))
{data=data.substring(0,(data.length-1));}
return data;
}

function countWords()
{
var input, wordList, output;
input=document.forms[0].elements[0].value;
input=trim(input);
wordList=input.split(/\s+/g);
output=wordList.length;
document.forms[0].elements[0].focus();
return output;
}

</SCRIPT>
</head>

<body bgcolor="#666666"
text="#00ff00"
link="#ffffcc"
vlink="#ffffcc">

Enter or paste text into the box below and press <b>count</b> to see
the total number of words.
<br>
<br>
<form onSubmit="return false">
<center>
<textarea rows="30" cols="100"></textarea>
<br>
<br>
<button onClick="alert('total words = ' + countWords())"
class="button">count</button>
</center>
</form>

</body>
</html>
Jul 20 '05 #1
2 1571
David wrote:
After getting some help working through my bugs, I have what seems to
be a robust, working word counter script. I post it here to benefit
others that might want this in the future and so that if I ever lose
my copy I can come back here to find it :) Some other scripts that I
used for inspiration failed when confronted with whitespace before the
string or miscalculated when encountering linefeeds and other
non-space spaces, so I made mine better. Definition of words for this
exercise is contiguous groups of characters separated by whitespace.
Maybe it will even be useful to somebody besides me.

Cheers!
--David

function trim(data)
{
while (/\s/.test(data.charAt(0)))
{data=data.substring(1,data.length);}
while (/\s/.test(data.charAt(data.length-1)))
{data=data.substring(0,(data.length-1));}
return data;
}
trim() would be better implemented as:

String.prototype.LTrim = new Function("return this.replace(/^\\s+/,'')")
String.prototype.RTrim = new Function("return this.replace(/\\s+$/,'')")
String.prototype.Trim = new Function("return
this.replace(/^\\s+|\\s+$/g,'')")

as shown in the FAQ at <url: http://jibbering.com/faq/#FAQ4_16 />
function countWords()
{
var input, wordList, output;
input=document.forms[0].elements[0].value;
input=trim(input);


Using the Trim() method outlined above, this would become:

input = input.Trim();

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #2
JRS: In article <67**************************@posting.google.com >, seen
in news:comp.lang.javascript, David <da***********@mindspring.com>
posted at Sun, 15 Feb 2004 23:51:19 :-
After getting some help working through my bugs, I have what seems to
be a robust, working word counter script. <!--Generated 02/12/04-->
this is an international newsgroup; please use internationally-
comprehensible notation. The above, in most countries, is 2nd December
2004. The correct form is 2004-02-12; or put the month as Feb and use
YYYY.
input=trim(input);
wordList=input.split(/\s+/g);
output=wordList.length;


What purpose does trim serve here? and /g?

I find
Q = X.replace(/\S+/g, 'a').replace(/\s+/g, '').length
to be significantly faster than
Q = X.split(/\s+/).length
for strings with many words. The latter creates many Objects.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eric A. Johnson | last post by:
Hi, Thanks for reading this. How do I list all possible permutations of a word? I've figured out how to get all the characters in alphabetical order. I then want to display every possible...
2
by: Anouar | last post by:
The point was to make a program that gives you the possibilty to enter a sentence and when you press "enter" it should ask a number for example when i press 4 It should take the 4th word out of...
0
by: John | last post by:
My application needs to capture the text the user has typed on a word document. I have control of the Word application/document with the following code: Word.Application word =...
0
by: hinew | last post by:
Hi. I have created macro for Microsoft word. However I need this to be run in visual baisc application. I need a program that ask user to select the word document which need to be processed....
7
by: onetitfemme | last post by:
Hi, for some reason Firefox is apparently inserting a new line somehow after some IPA symbols which I have written in their hexadecimal notation. Also, I would like to have two spaces by...
10
by: Antoine De Groote | last post by:
Hi there, I have a word document containing pictures and text. This documents holds several 'ABCDEF' strings which serve as a placeholder for names. Now I want to replace these occurences with...
10
by: Stephen.Schoenberger | last post by:
Hello, Sorry if this is not "exactly" a C topic but I thought this would be the best place to start. I need some guidance on working with bitmap images in ANSI C. I need to "read" in a bitmap...
6
by: boyindie86 | last post by:
Hi I have been fighting with this lump of code for the last week what I am trying to do is that I am passing words into passages of texts, and I want the system to go and find exact word matches...
3
by: dirknrw | last post by:
Hi, I have a tricky question!? :-) I'm using the perl -e command in order to match lines between two search patterns. The problem I have is, for the first example it is working, for the second...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.