Check if a String is a Valid Email Address in C
To check if a string is a valid email address in C, you can use various approaches such as employing regular expressions (using the POSIX regex library) or performing manual validation with string functions like strchr()
.
Example 1: Using POSIX Regular Expressions
This example demonstrates how to use the POSIX regex library to validate an email address. We will compile a regex pattern that matches the general structure of an email address and then use it to check our string.
main.c
#include <stdio.h>
#include <regex.h>
int main() {
const char *email = "example@test.com";
regex_t regex;
int ret;
// Regular expression pattern for basic email validation
const char *pattern = "^[A-Za-z0-9._%%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
// Compile the regex pattern
ret = regcomp(®ex, pattern, REG_EXTENDED);
if (ret) {
printf("Could not compile regex\n");
return 1;
}
// Execute regex to match the email string
ret = regexec(®ex, email, 0, NULL, 0);
if (!ret) {
printf("Valid email address\n");
} else {
printf("Invalid email address\n");
}
// Free memory allocated to the regex
regfree(®ex);
return 0;
}
Explanation:
- The variable
email
stores the email address to be validated. - A
regex_t
variable namedregex
is declared to hold the compiled regular expression. - A regex
pattern
is defined to match the general format of an email (e.g., characters before and after the ‘@’ symbol, and a domain suffix). regcomp()
compiles the regex pattern; if it fails, an error message is printed.regexec()
executes the regex on theemail
string. If it matches, the email is valid; otherwise, it is invalid.regfree()
frees the memory allocated for the regex.
Output:
Valid email address
Example 2: Manual Validation Using String Functions
This example shows a simple approach to validate an email address by manually checking if the string contains an ‘@’ symbol and a period (‘.’) after the ‘@’. This method uses basic string functions such as strchr()
.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char email[] = "user@example.com";
char *atSign = strchr(email, '@');
// Check if '@' exists in the string
if (atSign != NULL) {
// Check if there is a '.' after the '@'
char *dot = strchr(atSign, '.');
if (dot != NULL && dot > atSign) {
printf("Valid email address\n");
} else {
printf("Invalid email address\n");
}
} else {
printf("Invalid email address\n");
}
return 0;
}
Explanation:
- A character array
email
stores the email address to be validated. - The
strchr()
function searches for the ‘@’ symbol inemail
and returns a pointer to its first occurrence. - If the ‘@’ symbol is found, another
strchr()
call checks for the presence of a ‘.’ after the ‘@’. - If both symbols are present and in the correct order, the program prints “Valid email address”; otherwise, it prints “Invalid email address”.
Output:
Valid email address
Conclusion
In this tutorial, we explored two methods for validating an email address in C:
- Using POSIX Regular Expressions: Provides a robust solution with a regex pattern that checks for a valid email structure.
- Manual Validation: Uses basic string functions to check for the presence of essential characters, suitable for simple validations.