URL Validator in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <string.h>
int main() {
    char u[100];
    scanf("%s", u);
    printf((strstr(u, "http://") == u || strstr(u, "https://") == u) &&
           strstr(u, ".") ? "Valid" : "Invalid");
}

C Output

Input:
https://example.com

Output:
Valid


C++ Program

#include <iostream>
using namespace std;
int main() {
    string u; cin >> u;
    cout << ((u.find("http://") == 0 || u.find("https://") == 0) &&
             u.find('.') != string::npos ? "Valid" : "Invalid");
}

C++ Output

Input:  

Input:
ftp://mysite.com

Output:
Invalid



JAVA Program

import java.util.*;
public class Main {
    public static void main(String[] a) {
        String u = new Scanner(System.in).next();
        System.out.println((u.startsWith("http://") || u.startsWith("https://")) && u.contains(".") ? "Valid" : "Invalid");
    }
}

JAVA Output

Input:
http://localhost

Output:
Invalid



Python Program

u = input()
print("Valid" if u.startswith(("http://", "https://")) and '.' in u else "Invalid")

Python Output

Input:
http://google.com

Output:
Valid



In-Depth Explanation
Example
On the C version, the input https://example.com is tested. First, the program checks whether the URL begins with either http:// or https://. Second, it tests whether there's a. somewhere, which typically differentiates domain name from extension. As both conditions are met, it qualifies as valid.

However, if you attempt something like ftp://mysite.com or example.com, these will not pass because they do not begin with the proper protocol prefix.

Real-Life Analogy
Picture entering a web address into your browser. If the address does not begin with http:// or https://, your browser is not sure how to retrieve it. That is basically what this program does—it is an imitation of what a browser does when checking a URL.

Why It Matters
URL validation is crucial in:

Web forms

Login systems (email + redirect links)

API input validation

Security filters to preclude spurious links

Even simple URL structure check keeps users from inputting malformed data, which can crash your system or create security vulnerabilities.

Learning Insights
This code teaches you:

String operations (startsWith, contains, find, strstr)

Logical operators (&&, ||)

Practical but simple input validation

Minimal code with practical worth

It demonstrates how to apply several string conditions together to check for formatted input. And although this is a simple version, it's the starting point for more complex validators with regex or libraries.

Interview & Project Relevance
This question type is prevalent in:

String manipulation challenges

Web application development exercises

Input filtering and sanitizing in full-stack positions

In actual applications, this logic is the foundation of:

Web form validators

Signup/login validators

URL shorteners

Redirect handlers

A URL validator is a real-world example that demonstrates how to work with formatted input using basic string manipulations. It is widely applied in web programming, backend checks, and security layers of systems. The shortest, most concise working code in C, C++, Java, and Python, this solution provides you with a solid foundation for designing smarter, more secure systems that deal with realistic input in a smooth way.