How to count the letters in a text with Javascript?

Posted by Doguhanca on Stack Overflow See other posts from Stack Overflow or by Doguhanca
Published on 2013-11-09T15:40:53Z Indexed on 2013/11/09 15:53 UTC
Read the original article Hit count: 208

Filed under:
|
|
|
|

I am currently trying to write a ''web application'' that has a simple text area inside, in which I want the letters of the text written to be pointed out.

For example, if I write:

''How old are you? I am 19 years old''

I need a code to tell me how many 'A's and 'Y's and 'D's (and all letters of the alphabet from 0-26) are used in this sentence when I press a button on a HTML/ CSS page.

Could you please tell me what I must write into my .JS file and what I should write into my .HTML file to do this with a click of a button when something is written in the ?

I hope my explanation was detailed enough. Thanks!

Edit (I'm very sorry for the problems I caused) - What I have done so far looks like this:

HTML:

<link rel="stylesheet" type="text/css" href="theme.css">
<meta charset="utf-8"> 

<script src="test.js" type="text/javascript"></script>

<div class='header'>
Al.Fa.Be
</div>

<div class='yaz'>
<textarea></textarea>

</div>

<div class='description'>
<a href='http://www.google.com'>Ara</a>
</div>

<div class='description2'>
<input id="clickMe" type="button" value="Hesapla" onclick="doFunction();" />
</div>

CSS:

body{
background:white;
}

selection{
background:#CCC;
}

#clickMe{
background:#CCC;
border:1px solid #333;

}

.header{
font-size:70px;
font-weight:bold;
font-family:Arial;
color:#333;
margin-left:580px;
padding-top:200px;
}

textarea{
width:1210px;
height:40px;
color:black;
margin-top:20px;
margin-left:100px;
padding-left:10px;
padding-top:10px;
font-size:18px;
font-family:Arial;
}

.description{
background:#f2f2f2;
padding:6px;
width:50px;
text-align:center;
border:1px solid #ddd;
font-family:Arial;
margin-left:620px;
margin-top:20px;
font-size:14px;
}

.description a{
color:#555;
text-decoration:none;
}

.description2{
background:#f2f2f2;
padding:6px;
width:60px;
text-align:center;
border:1px solid #ddd;
font-family:Arial;
margin-left:750px;
margin-top:-30px;
font-size:14px;
}

.description2 a{
color:#555;
text-decoration:none;
}

.yaz{
color:white;
}

Javascript:

// Input name. Count number of alphabets a-z
class program                                                          
{
    public static void main(String[] args) 
    {
        String name = args[0];
        int count[] = new int[29];
        int i,p;
        int n = name.length();
        name = name.toUpperCase();
        char c;
        for (i=0; i<29; i++) 
        {
            count[i] = 0;   
        }
        for (i=0; i<n; i++) 
        {
            c = name.charAt(i);
            p = (int) c;
            count[p-65]++;
        }
        for (i=0; i<29 ; i++) 
        {
            if (count[i] >0) 
            {
                System.out.println((char)(i+65) + " occurs " + count[i] + " times");
            }
        }
    }
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html