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.