11.13 Write a script( HTML5) that inputs several lines of text and a search char
ID: 3890464 • Letter: 1
Question
11.13 Write a script( HTML5) that inputs several lines of text and a search character and uses String method
indexOf to determine the number of occurrences of the character in the text.
11.14 Write a script based on the program in Exercise 11.13 that inputs several lines of text and uses String method indexOf to determine the total number of occurrences of each letter of the alphabet in the text. Uppercase and lowercase letters should be counted together. Store the totals for each letter in an array, and print the values in tabular format in an HTML5 textarea after the totals have been determined.
Explanation / Answer
11.13
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>CountIt</title>
<script type="text/javascript">
<!--
function search1()
{
var str1 = document.getElementById("string1").value;
var str2 = document.getElementById("string2").value;
var len=str1.length;
var count=0;
var search=str1.indexOf(str2);
for(i=0;i<len;i++)
{
if(parseInt(search)!= -1)
{
count++;
var search=str1.indexOf(str2,parseInt(search)+1);
}
}
document.getElementById("ans").value= str2 + " Occurs " + count + " times";
}
// -->
</script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> </td>
<h1>Exercise 11.13 CountIt</h1>
<hr />
<p>Press F5 or Refresh to load script again. This is a program to determine the number of occurrences of the character in the text .</p>
<form name = "search" action = "">
<table border = "1">
<caption>Search</caption>
<tr><td>Enter any String</td>
<td><input id = "string1" type = "text" />
</td></tr>
<tr><td>Enter search Character</td>
<td><input id = "string2" type = "text" />
</td></tr>
<tr><td>Search Result</td>
<td><textarea id="ans">
</textarea>
</td></tr>
<tr><td><input type = "button" value = "SearchIt"
/></td></tr>
</table>
</form>
</tr>
</table>
</body>
</html>
11.14
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>CountThem</title>
<script type="text/javascript">
<!--
function search1()
{
var str1 = document.getElementById("string1").value;
var str2 = new Array ("a", "b", "c" , "d", "e", "f", "g" , "h" , "i", "j", "k" , "l" , "m", "n" , "o" , "p" , "q", "r", "s", "t" , "u", "v", "w" , "x", "y" , "z");
var str11=str1.toLowerCase();
var len=str11.length;
document.getElementById("ans").value="" ;
for(i=0;i<26;i++)
{
var count=0;
var search=str11.indexOf(str2[i]);
for(j=0;j<len;j++)
{
if(parseInt(search)!= -1)
{
count++;
var search=str11.indexOf(str2[i],parseInt(search)+1);
}
}
if(count!=0)
{
document.getElementById("ans").value= document.getElementById("ans").value + " " + str2[i] + " Occurs " + count + " times";
}
}
}
// -->
</script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> </td>
<h1>CountThem</h1>
<hr />
<p>Press F5 or Refresh to load script again. This is a program to determine the total number of occurrences of each letter of the alphabet in the text .</p>
<form name = "search" action = "">
<table border = "1">
<caption>Search</caption>
<tr><td>Enter any String</td>
<td><input id = "string1" type = "text" />
</td></tr>
<tr><td>Search Result</td>
<td><textarea id="ans" rows="26" cols="20">
</textarea>
</td></tr>
<tr><td><input type = "button" value = "CountThem"
/></td></tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.