1113 Write a script HTML5 that inputs several lines of text

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.

Solution

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

<?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 style=\"width: 100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td valign=\"top\">&nbsp;</td>
<h1>Exercise 11.13 CountIt</h1>
<hr />

<p> 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 name = \"string1\" type = \"text\" />
</td></tr>
<tr><td>Enter search Character</td>
<td><input name = \"string2\" type = \"text\" />
</td></tr>
<tr><td>Search Result</td>
<td><textarea name=\"ans\">
</textarea>
</td></tr>
<tr><td><input type = \"button\" value = \"SearchIt\" onclick = \"search1()\" /></td></tr>
</table>
</form>
</tr>
</table>

</body>
</html>

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

<?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 style=\"width: 100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td valign=\"top\">&nbsp;</td>
<h1>CountThem</h1>
<hr />

<p> 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 name = \"string1\" type = \"text\" />
</td></tr>
<tr><td>Search Result</td>
<td><textarea name=\"ans\" rows=\"26\" cols=\"20\">
</textarea>
</td></tr>
<tr><td><input type = \"button\" value = \"CountThem\"
onclick = \"search1()\" /></td></tr>
</table>
</form>
</td>
</tr>
</table>

</body>
</html>

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site