Web Technologies
Slip6
Q.1) Write a JavaScript program to read a character string from user and perform the following functions:
a. Accept a character from user and count the number of occurrences of that character in the string.
b. Accept a Position from user and print the character at specified position. (Use promt() function for accepting a character) [Marks 30]
a. Accept a character from user and count the number of occurrences of that character in the string.
b. Accept a Position from user and print the character at specified position. (Use promt() function for accepting a character) [Marks 30]
Solution
slip6.html
<html><head>
<script language="javascript">
function partichar()
{
var str=document.getElementById("str1").value;
var c=parseInt(prompt("Enter position to display character"))
alert(str.charAt(c-1));
}
function cout()
{
var str=document.getElementById("str1").value;
var c=prompt("Enter character");
var i=0;
var cnt=0;
for(i=0;i<str.length;i++)
{
if(str[i]==c)
cnt++;
}
alert("No of occurance="+cnt);
}
</script>
</head>
<body>
<table border="0">
<tr>
<th> Enter the string:</th>
<td><input type="text" name=str1 id=str1></td>
</tr>
<tr>
<td><input type="radio" name=chk value=1 onClick="cout()" ></td>
<td>Accept The character from user and cout it occurance</td>
</tr>
<tr>
<td><input type="radio" name=chk value=2 onClick="partichar()"></td>
<td>Print the Character at the perticular position</td>
</tr>
</body>
</html>
Tags:
Web Technologies