Introduction
A URL shortener is a tool that converts long URLs into shorter, more manageable links that are easy to share and remember.
In this tutorial, we will create a basic URL shortener using the C programming language. The program will take a long URL as input
and generate a shortened version. This is a great way to learn about string manipulation and file handling in C.
Objective
The objective of this program is to implement a simple URL shortener that maps long URLs to short, custom identifiers.
We’ll be storing the long URL and its corresponding short URL in a file for future reference.
Code
#include
#include
#include
#define MAX_URL_LENGTH 2048
#define SHORT_URL_LENGTH 6
// Function to generate a short URL (simple hash)
void generateShortURL(char *longURL, char *shortURL) {
unsigned long hash = 5381;
int c;
// Generate a simple hash from the long URL
while ((c = *longURL++)) {
hash = ((hash << 5) + hash) + c;
}
// Convert hash to a string of characters
snprintf(shortURL, SHORT_URL_LENGTH, "%lx", hash % 1000000); // Limit the length
}
// Function to store the long and short URL pair
void storeURL(char *longURL, char *shortURL) {
FILE *file = fopen("urls.txt", "a");
if (file == NULL) {
printf("Could not open file for saving URL.\n");
return;
}
fprintf(file, "Long URL: %s\nShort URL: %s\n\n", longURL, shortURL);
fclose(file);
}
int main() {
char longURL[MAX_URL_LENGTH], shortURL[SHORT_URL_LENGTH];
// Input the long URL
printf("Enter the long URL: ");
fgets(longURL, MAX_URL_LENGTH, stdin);
// Remove the newline character
longURL[strcspn(longURL, "\n")] = 0;
// Generate short URL
generateShortURL(longURL, shortURL);
// Display the short URL
printf("Short URL: http://short.ly/%s\n", shortURL);
// Store the URL pair in a file
storeURL(longURL, shortURL);
return 0;
}
Explanation of the Program
This program consists of a few important parts:
- generateShortURL function: This function takes a long URL as input and generates a hash-based short URL. It simply creates a hash from the URL and then converts it into a string of numbers to create a unique short URL.
- storeURL function: This function stores the long URL and its corresponding short URL in a text file named
urls.txt
. This allows us to persist the mapping between long and short URLs. - main function: The main function drives the program. It prompts the user to enter a long URL, generates a short URL using the
generateShortURL
function, displays it to the user, and then stores the mapping in a file using thestoreURL
function.
How to Run the Program
To run this program, follow these steps:
-
- Copy the code into a C file, for example,
url_shortener.c
. - Open a terminal and navigate to the directory where the file is saved.
- Compile the program using a C compiler (e.g., GCC):
- Copy the code into a C file, for example,
gcc url_shortener.c -o url_shortener
-
- Run the compiled program:
./url_shortener
- Enter the long URL when prompted, and the program will generate and display a short URL. It will also store the mapping in the
urls.txt
file.