Hi, use this piece of code:
var contador = 0;
var strInput = prompt("Please enter a few words:", "")
indice = strInput.indexOf("i");
while (indice!=-1) {
contador++;
indice = strInput.indexOf("i",indice+1);
}
document.write("there are " + contador + " instances of the letter \'i\' in
your string.");
Regards,
Federico García
>From: Manon Lechasseur <m_lechasseur@y...>
>Reply-To: "javascript" <javascript@p...>
>To: "javascript" <javascript@p...>
>Subject: [javascript] number of instances of character in string
>Date: Sun, 2 Jun 2002 18:05:32 -0700 (PDT)
>
>Hi everyone,
>
>How can I display the number of times a character
>occurs in a string entered by the user? In this
>particular case, the character I'm looking for is 'i'.
>
>I've tried various String objects and
>conditional/loops, but to no avail. I've probably
>overlooked something obvious!! Be kind, I'm just
>starting out and loops aren't my forte :)
>
>Here's the code I have so far:
>
>var strInput = prompt("Please enter a few words:", "")
>
>if (strInput.search("i") == true)
>{
>document.write("The letter \'i\' is present in your
>string.");
>document.write("There are ");
>
>for (var i=0; i <= strInput.length - 1; i++) {
>
>document.write(i, " instances of the letter \'i\' in
>your string.");
>}
>}
>
>else
>{
>document.write("There is no instance of the letter
>\'i\' in your string.");
>}
>
>Thank you in advance for your help.
>
>Manon
>
>__________________________________________________
>Do You Yahoo!?
>Yahoo! - Official partner of 2002 FIFA World Cup
>http://fifaworldcup.yahoo.com
>
>
>---
>
>Improve your web design skills with these new books from Glasshaus.
>
>Usable Web Menus
>http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
>r-20
>Constructing Accessible Web Sites
>http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
>r-20
>Practical JavaScript for the Usable Web
>http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
>r-20
_________________________________________________________________
Envíe y reciba su correo de Hotmail desde el móvil: http://mobile.msn.com