Initial letters of the words in a string

S

Swifty

Given a string of words such as "The quick brown fox jumps over the
lazy dog", how easy would it be to generate a string containing the
initial letters of the words, in lower case, e.g. 'tqbfjotld'

As it happens, the string will come from the content (value) of one
<INPUT TYPE=TEXT> control, and the output will replace the content
(value) of another INPUT control on a user action, such as a mouse
click.

I don't have a page to demonstrate how far I've already got, as the
idea only just occurred to me. I can manage the HTML, and the
JavaScript to get the value of the first INPUT control, and then set
the content of the second INPUT control.

What I'm looking for is the JavaScript equivalent of my initials()
function that I have in my REXX subroutines library:

Inits = ''
Do I = 1 to words(string)
Inits = Inits || left(word(string,I),1)
End
 
E

Evertjan.

Swifty wrote on 08 sep 2011 in comp.lang.javascript:
Given a string of words such as "The quick brown fox jumps over the
lazy dog", how easy would it be to generate a string containing the
initial letters of the words, in lower case, e.g. 'tqbfjotld'

As it happens, the string will come from the content (value) of one
<INPUT TYPE=TEXT> control, and the output will replace the content
(value) of another INPUT control on a user action, such as a mouse
click.

I don't have a page to demonstrate how far I've already got, as the
idea only just occurred to me. I can manage the HTML, and the
JavaScript to get the value of the first INPUT control, and then set
the content of the second INPUT control.

What I'm looking for is the JavaScript equivalent of my initials()
function that I have in my REXX subroutines library:

Inits = ''
Do I = 1 to words(string)
Inits = Inits || left(word(string,I),1)
End
... how easy would it be ....

It is easy in Javascript.
We should, however, not do your schoolwork.

Remember that Javascript has Regular Expressions,
so amaze your teacher with a fast and compact solution,
using just on line without loops.
 
J

Jukka K. Korpela

Given a string of words such as "The quick brown fox jumps over the
lazy dog", how easy would it be to generate a string containing the
initial letters of the words, in lower case, e.g. 'tqbfjotld'

It depends on the exact definition of "word" and "letter", but assuming
very simple definitions (a word is a maximal sequence of non-space
letters, and any non-space character qualifies as "first letter"), quite
simple:

<!doctype html>
<title>Extract first letters</title>
<input id=data size=50 style="width: 100%">
<input type=button value=Extract onclick=
"document.getElementById('result').value =
firstLetters(document.getElementById('data').value) ">
<p>
<input id=result>
<script>
function firstLetters(str) {
var words = str.split(' ');
var result = '';
var i;
for(i = 0; i < words.length; i++) {
result += words.charAt(0).toLowerCase();
}
return result;
}
</script>

I have assumed that the first character needs to be converted to
lowercase. This part too is easy _unless_ you need to consider the case
where the text is in Turkish, Azeri, or another language that
distinguishes between normal i and dotless i.

If "letter" means literally "letter" as an alphabetic character, then
things get rather difficult. There is no direct way in JavaScript to
decide whether a given character is alphabetic. It would still be
doable, but laborious, and you would need to fix the concept of "letter"
_somehow_ (e.g., "a character defined as a letter in Unicode version 6.0").

Then again, if "letter" means just any simple Latin letter from "a" to
"z", then things are fairly simple.
What I'm looking for is the JavaScript equivalent of my initials()
function that I have in my REXX subroutines library:

Inits = ''
Do I = 1 to words(string)
Inits = Inits || left(word(string,I),1)
End

It was _ages_ ago when I used REXX, and not much, but I guess that the
code works with rather simple concepts of "word" and "letter". But
"word" might differ from the simplest definition - for example, would
the "words" in "Hello, world!" be "Hello," and "world!" or "Hello" and
"world"?
 
T

Thomas 'PointedEars' Lahn

Swifty said:
Given a string of words such as "The quick brown fox jumps over the
lazy dog", how easy would it be to generate a string containing the
initial letters of the words, in lower case, e.g. 'tqbfjotld'

Very easy. Assuming that "word" refers to the ECMAScript meaning:

"The quick brown fox jumps over the lazy dog"
.match(/\b\S/g).join("").toLowerCase()


PointedEars
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 08 sep 2011 in comp.lang.javascript:
Very easy. Assuming that "word" refers to the ECMAScript meaning:

"The quick brown fox jumps over the lazy dog"
.match(/\b\S/g).join("").toLowerCase()

s = s.replace(/\s*(\S)\S*(\s+|$)/g,'$1').toLowerCase()
 
R

RobG

Thomas 'PointedEars' Lahn wrote on 08 sep 2011 in comp.lang.javascript:




s = s.replace(/\s*(\S)\S*(\s+|$)/g,'$1').toLowerCase()

why not:

s.replace(/\b(\w)\w*\s*/g,'$1').toLowerCase()

Homework complete - until the asked "how does it work?"
 
S

Swifty

It depends on...

Yes, blank delimited words, probably nothing but a-z and maybe 0-9.

So far, the webpage concerned is used only in the UK and South Africa,
but could spread anywhere where my corporate employer does business.
I have safety net, however; I'm retiring in under two years, so my
"stuff" is unlikely to spread much further while I'm still supporting
it.
If they run into problems after I'm gone, I'll recommend that they ask
you; the hourly rates for consultancy are attractive. :)

Your example does exactly what I was hoping for, so, sometimes you
don't have to be careful what you wish for.
 
E

Evertjan.

RobG wrote on 09 sep 2011 in comp.lang.javascript:
why not:

s.replace(/\b(\w)\w*\s*/g,'$1').toLowerCase()

Not, because starting spaces are not filtered out.
Homework complete - until the asked "how does it work?"

Indeed, the op will not have learned much.
 
S

Swifty

Homework complete - until the asked "how does it work?"

As a matter of principle, I don't put into production any code that I
couldn't explain to a bright child.

As for all the "homework" comments: of course it's homework; I work
from home. Here's the view from my "office" over the top of my
display: http://swiftys.org.uk/images/home_office.jpg

It sure is nice to have you people helping me with my homework. Just
don't expect to help me with spending my salary. I've got a wife for
that :)
 
S

Scott Sauyet

Swifty said:
It sure is nice to have you people helping me with my homework. Just
don't expect to help me with spending my salary. I've got a wife for
that :)

Thanks for your good humor about this. It does *sound* like a
homework problem. But your posting equivalent code in Rexx probably
should have lent you some legitimacy (unless CS programs are now
teaching Rexx as a prerequisite to JS :) )

I am amused by those who posted solutions but still thought it to be
homework, though. Why not post hints and suggestions until the OP
actually coughs up some code?

-- Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top