Initial letters of the words in a string

Discussion in 'Javascript' started by Swifty, Sep 8, 2011.

  1. Swifty

    Swifty Guest

    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

    --
    Steve Swift
    http://www.swiftys.org.uk/swifty.html
    http://www.ringers.org.uk
    Swifty, Sep 8, 2011
    #1
    1. Advertising

  2. Swifty

    Evertjan. Guest

    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.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Sep 8, 2011
    #2
    1. Advertising

  3. 8.9.2011 18:27, Swifty wrote:

    > 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"?


    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Sep 8, 2011
    #3
  4. Swifty wrote:

    > 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
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Sep 8, 2011
    #4
  5. Swifty

    Evertjan. Guest

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

    > Swifty wrote:
    >
    >> 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()


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


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Sep 8, 2011
    #5
  6. Swifty

    RobG Guest

    On Sep 9, 3:47 am, "Evertjan." <> wrote:
    > Thomas 'PointedEars' Lahn wrote on 08 sep 2011 in comp.lang.javascript:
    >
    > > Swifty wrote:

    >
    > >> 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()

    >
    > 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?"

    --
    Rob
    RobG, Sep 9, 2011
    #6
  7. Swifty

    Swifty Guest

    On Thu, 08 Sep 2011 19:18:22 +0300, "Jukka K. Korpela"
    <> wrote:

    >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.

    --
    Steve Swift
    http://www.swiftys.org.uk/swifty.html
    http://www.ringers.org.uk
    Swifty, Sep 9, 2011
    #7
  8. Swifty

    Evertjan. Guest

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

    > On Sep 9, 3:47 am, "Evertjan." <> wrote:
    >> Thomas 'PointedEars' Lahn wrote on 08 sep 2011 in comp.lang.javascript:
    >>
    >> > Swifty wrote:

    >>
    >> >> 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()

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

    >
    > 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.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Sep 9, 2011
    #8
  9. Swifty

    Swifty Guest

    On Thu, 8 Sep 2011 19:57:38 -0700 (PDT), RobG <>
    wrote:

    >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 :)

    --
    Steve Swift
    http://www.swiftys.org.uk/swifty.html
    http://www.ringers.org.uk
    Swifty, Sep 9, 2011
    #9
  10. Swifty

    Scott Sauyet Guest

    Swifty wrote:
    > 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
    Scott Sauyet, Sep 9, 2011
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Merrigan
    Replies:
    4
    Views:
    565
    Chris
    Dec 14, 2007
  2. BerlinBrown
    Replies:
    6
    Views:
    4,477
  3. limit
    Replies:
    1
    Views:
    258
  4. limit
    Replies:
    4
    Views:
    338
    alex23
    May 1, 2009
  5. Venugopal
    Replies:
    11
    Views:
    1,518
    Tassilo v. Parseval
    Nov 5, 2003
Loading...

Share This Page