Article 6DKED Check if dfa recognizes email addresses.

Check if dfa recognizes email addresses.

by
ajiten
from LinuxQuestions.org on (#6DKED)
Want to get solution to the assignment given here, in C, and then in C++ and Python.
Also, want to do the same later using the table based approach, rather than just remembering the state information, of the dfa implcitly in the program.

But, for kickstart, wanted the exercise to start with the following problem:

1. Check if the first character of the email id string is an alphabet or not. If not, then the email is Invalid.
2. Now traverse over the string email to find the position the @" and ." If @" or ." is not present then the email is Invalid.
3. If ." is not present after @" then the email is Invalid.
4. If ." is the last character of the string email then the email id is Invalid.
5. Otherwise, the email is Valid.

But, am facing issues in compilation, and the answer gives the same length of string always; i.e. 8

Code:#include <stdio.h>
#include <stdbool.h>

// Function to check the character is an alphabet or not
bool isChar(char c)
{
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}

// Function to check the character is an digit or not
bool isDigit(const char c)
{
return (c >= '0' && c <= '9');
}

// Function to check the character is a letter (alphabet) or not
bool isLetter(const char c)
{
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}

// Function to check email id is valid or not
bool is_valid(char* email)
{
// Check the first character is an alphabet or not
if (!isChar(email[0])) {

// If it's not an alphabet email id is not valid
return 0;
}
// Variable to store position of At and Dot
int At = -1, Dot = -1;

// Traverse over the email id string to find position of Dot and At
for (int i = 0; i < (sizeof(email)/sizeof(char)); i++) {

if (i==0) printf("%d", sizeof(email)/sizeof(char));

// If the character is '@'
if (email[i] == '@') {

At = i;
}

// If character is '.'
else if (email[i] == '.') {

Dot = i;
}
}

// If At or Dot is not present
if (At == -1 || Dot == -1)
return 0;

// If Dot is present before At
if (At > Dot)
return 0;

// If Dot is present at the end
return !(Dot >= ((sizeof(email)/sizeof(char)) - 1));
}

// Driver Code
int main()
{
// Given string email
char* email = "abcdefghi-team@lfl.org";

// Function Call
bool ans = is_valid(email);

// Print the result
if (ans) {
printf("%c : valid", email);
}
else {
printf("%c : invalid", email);
}

return 0;
}
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments