How to find a specific keyword occurred in Website

Spread the love

I went to an job interview and they asked me to do the following 2 coding in 30 min.

Find a keyword occurrence in  a specific website for its URL. There should be 2 input fields. One will for keyword and another will be for URL input. On submit it will show the occurrence of the keyword in the given website.

Well may be there are many possible good ways to do this but I know php and this seemed to me the easiest way.

PHP:

1
2
3
4
5
6
7
8
9
 
<?php
if(isset($_POST['find'])){
 
$str = file_get_contents($_POST['url_input']);
 
$result = (substr_count(strip_tags($str),$_POST['keyword_input']));
 
?>

HTML :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<html>
<head>
 
<title>keyword Finder</title>
 
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
	<h2> Find the Keyword Occurrence in a website </h2>
	<hr>
	<h3 class="alert alert-success">
        <?php echo "'". $_POST['keyword_input']."' Occurred in the website: ".$_POST['url_input']." total: "; echo $result." times"; }?>
        </h3>
	<hr>
	<div class="row">
		<div class="col col-lg-4 col-sm-8">
		<form action="" method="post">
		  <div class="form-group">
			<label for="keywordfinder">Enter Keyword</label>
			<input type="text" class="form-control" name="keyword_input" aria-describedby="emailHelp" placeholder="Enter Keyword">
		  </div>
		  <div class="form-group">
			<label for="givenurl">Enter URL</label>
			<input type="url" class="form-control" name="url_input" placeholder="Enter URL (http://yourwebsite.com)">
		  </div>
		  <button type="submit" class="btn btn-primary" value="Find Keyword Occurrence" name="find">Submit</button>
		</form>
		</div>
	</div>
</div>

Output :

Let’s know what I did here:

file_get_contents() reads a file into a string. It will use memory mapping techniques to find string. I used it to read the url and find the specific keyword given occurred in the website.
Syntax:
file_get_contents(path,include_path,context,start,max_length) — Here ‘path’ is the required field


substr_count() function counts the number of times a substring occurs in a string. This function is Case Sensitive.
Syntax:
substr_count(string,substring,start,length) — Here ‘string’ and ‘substring’ are required fields.


strip_tags() function strips a string from HTML, XML, and PHP tags. Strip means remove the covered things.
Syntax:
strip_tags(string,allow) — Here ‘string’is the required field.

Find the exponential value of a number. You can use C++/python/Java. Use recursive method

I did the coding in Java. Here is the code I tried there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.*;
 
public class ExpFinder {
    static void getExponent(){
        Scanner noor = new Scanner(System.in);
        System.out.println("Enter A Number: ");
        double inputVal = noor.nextDouble();
        System.out.println("Exponent value of "+ inputVal +" is: "+ Math.getExponent(inputVal));
        System.out.println("Square Root of the value  "+ inputVal +" is: "+ Math.cbrt(inputVal));
        System.out.println("Cubic Root of the value  "+ inputVal +" is: "+ Math.sqrt(inputVal));
 
    }
 
    public static void main(String args[]) {
        getExponent();
    }
}
 
Output: 
Enter A Number: 25.0
Exponent value of 25.0 is: 4
Square Root of the value  25.0 is: 2.924017738212866
Cubic Root of the value  25.0 is: 5.0