FY B.sc IT Practical 3(E) : Write a program in JavaScript to accept a sentence from the user and display the number of words in it. (Do not use split () function).
<!DOCTYPE html>
<html>
<head>
<title>Without using split function</title>
<script>
var str=prompt("Enter the sentence=","");
var count=0;
for(i=0;i<str.length;i++)
{
if(str.charAt(i,1)==" " && str.charAt(i+1,1)!=" ")
count++;
}
document.write("Number of words are="+(count+1));
</script>
</head>
<body>
</body>
</html>
Comments
Post a Comment