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

</>
Copy
#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(&regex, pattern, REG_EXTENDED);
    if (ret) {
        printf("Could not compile regex\n");
        return 1;
    }

    // Execute regex to match the email string
    ret = regexec(&regex, email, 0, NULL, 0);
    if (!ret) {
        printf("Valid email address\n");
    } else {
        printf("Invalid email address\n");
    }

    // Free memory allocated to the regex
    regfree(&regex);
    return 0;
}

Explanation:

  1. The variable email stores the email address to be validated.
  2. A regex_t variable named regex is declared to hold the compiled regular expression.
  3. 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).
  4. regcomp() compiles the regex pattern; if it fails, an error message is printed.
  5. regexec() executes the regex on the email string. If it matches, the email is valid; otherwise, it is invalid.
  6. 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

</>
Copy
#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:

  1. A character array email stores the email address to be validated.
  2. The strchr() function searches for the ‘@’ symbol in email and returns a pointer to its first occurrence.
  3. If the ‘@’ symbol is found, another strchr() call checks for the presence of a ‘.’ after the ‘@’.
  4. 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:

  1. Using POSIX Regular Expressions: Provides a robust solution with a regex pattern that checks for a valid email structure.
  2. Manual Validation: Uses basic string functions to check for the presence of essential characters, suitable for simple validations.