C Examples

WAP in C to Print E-mail Addresses found in any text document!

The Program in C to Print E-mail Addresses found in any text document is given below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_EMAIL_LEN 256

int main() {
    char file_name[256];
    char email[MAX_EMAIL_LEN];
    FILE *file_ptr;
    int i, j;

    printf("Enter the name of the text file: ");
    scanf("%s", file_name);

    file_ptr = fopen(file_name, "r");
    if (file_ptr == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    while (fscanf(file_ptr, "%s", email) != EOF) {
        for (i = 0; email[i] != '\0'; i++) {
            if (email[i] == '@') {
                for (j = i; email[j] != '\0'; j++) {
                    if (email[j] == '.') {
                        printf("%s\n", email);
                        break;
                    }
                }
            }
        }
    }

    fclose(file_ptr);
    return 0;
}

Output:

Enter the name of the text file: emails.txt

My name is John and my email is [email protected]. Please contact me at this address if you have any questions.

Pro-Tips💡

This program prompts the user to input the name of a text file, and then it opens the file and scans through each word in the file.

If a word contains the “@” symbol, it then checks if it also contains a “.” symbol, which are typical characteristics of email addresses.

If it does, it prints the word out as an email address.


Please note that it only scans for ‘@’ and ‘.’ and it doesn’t validate the email address if it’s correct or not, it is just a simple program to find the email address in the text document.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X