Hello Programmers, In this post, you will learn how to solve HackerRank Building a Smart IDE: Programming Language Detection Solution. This problem is a part of the Regex HackerRank Series.
One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the Regex HackerRank Solutions using CPP, JAVA, PYTHON, JavaScript & PHP Programming Languages.

HackerRank Building a Smart IDE: Programming Language Detection Solution
Problem
We are trying to hack together a smart programming IDE. Help us build a feature which auto-detects the programming language, given the source code. There are only three languages which we are interested in “auto-detecting”: Java, C and Python.
We will provide you with links to a few short or medium size programs for Java, C and Python. In case you aren’t familiar with some of these languages, these samples will help you make observations about the lexical structure and syntax of these programming languages. These sample programs are only for your manual inspection. You cannot read or access these sample-programs from the code you submit.
After this, you will be provided with tests, where you are provided the source code for programs – or partial code snippets, but you do not know which language they are in. For each test, try to detect which language the source code is in.
You might benefit from using regular expressions in trying to detect the lexical structure and syntax of the programs provided.
Sample Programs to Understand the Lexical Structure of different Programming Languages
Sample Programs and Code Snippets in C
Sample Programs and Code Snippets in Java
Sample Programs and Code Snippets in Python
INPUT FORMAT
Source code of a program, or a code snippet, which might be in C, Java or Python.
OUTPUT FORMAT
Just one line containing the name of the Programming language which you have detected: This might be either C or Java or Python.
SAMPLE INPUT
import java.io.*;
public class SquareNum {
public static void main(String args[]) throws IOException
{
System.out.println(“This is a small Java Program!“);
}
}
SAMPLE OUTPUT
Java
HackerRank Building a Smart IDE: Programming Language Detection Solution in Cpp
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <string> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ string s; int a=0; bool b=true; for(int i=0;i<15;i++){ getline(cin,s); a=s.find("java"); if(a!=string::npos){ cout<<"Java"<<endl; b=false; break; } a=s.find("stdio"); if(a!=string::npos){ cout<<"C"<<endl; b=false; break; } } if(b)cout<<"Python"<<endl; return 0; }
HackerRank Building a Smart IDE: Programming Language Detection Solution in Java
import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<String> program = new ArrayList<String>(); String line; try { while ((line = in.nextLine()) != null) { if (!line.equals("")) { program.add(line.trim()); } } } catch (NoSuchElementException e) { } String imp = program.get(0); if ((imp.startsWith("package") || imp.startsWith("import")) && imp.endsWith(";")) { System.out.println("Java"); return; } if (imp.startsWith("#include")) { System.out.println("C"); return; } boolean python = true; for (String s : program) { if (python && s.endsWith(";")) { python = false; break; } } if (python) { System.out.println("Python"); return; } for (String s : program) { if (s.contains("class ") || s.contains("interface ")) { System.out.println("Java"); return; } } System.out.println("C"); } }
HackerRank Building a Smart IDE: Programming Language Detection Solution in Python
import fileinput stdin = [line.strip() for line in fileinput.input()] tokens = [] for line in stdin: tokens.extend(line.split()) tokens = set(tokens) if 'class' in tokens and '{' in tokens: print('Java') elif '{' in tokens: print('C') else: print('Python')
HackerRank Building a Smart IDE: Programming Language Detection Solution in JavaScript
function processData(input) { //Enter your code here if(checkPython(input)){ console.log("Python"); return; } if(checkJava(input)){ console.log("Java"); return; } if(checkC(input)){ console.log("C"); return; } console.log("C"); } function checkPython(str){ if(/\sdef\s[a-zA-Z0-9]*\(/g.test(str)){ return true; } else if(/print\s/g.test(str)){ return true; }else{ return false; } } function checkJava(str){ return /System\./g.test(str); } function checkC(str){ if(/\s\*[a-zA-Z0-9]/g.test(str)){ return true; } else if(/[a-zA-Z0-9]->[a-zA-Z0-9]/g.test(str)){ return true; }else{ return false; } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
HackerRank Building a Smart IDE: Programming Language Detection Solution in PHP
<?php $python = true; for($i=0;$i<15;$i++){ $line = trim(fgets(STDIN)); $temp = strpos($line, "java"); if ($temp!==false){ $python=false; echo "Java"; break; } $temp = strpos($line, "stdio"); if ($temp!==false){ $python=false; echo "C"; break; } } if($python){ echo "Python"; } ?>
Disclaimer: This problem (Building a Smart IDE: Programming Language Detection) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.