LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2023, 06:05 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 376

Rep: Reputation: 4
Check if dfa recognizes email addresses.


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;
}

Last edited by ajiten; 08-06-2023 at 06:07 AM.
 
Old 08-06-2023, 07:08 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
https://c-faq.com/aryptr/aryparmsize.html

Quote:
Q: Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? I have a test routine

Code:
	f(char a[10])
	{
		int i = sizeof(a);
		printf("%d\n", i);
	}
and it prints 4, not 10.

A: The compiler pretends that the array parameter was declared as a pointer (that is, in the example, as char *a; see question 6.4), and sizeof reports the size of the pointer. See also questions 1.24 and 7.28.
(The FAQ example reports the output on a 32-bit machine, which is they are getting 4 rather than 8.)
 
1 members found this post helpful.
Old 08-06-2023, 07:13 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,872
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
@OP: strlen(3) is your friend. (Call in once: before the loop.]
 
Old 08-06-2023, 12:31 PM   #4
ajiten
Member
 
Registered: Jun 2023
Posts: 376

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
@OP: strlen(3) is your friend. (Call in once: before the loop.]
Sorry, couldn't understand the meaning of '3' inside brackets. Though on googling with the query term: strlen(3); get link.

Have replaced the code at line #35, 61 (apart from the inconsequential line #37) to get the correct output, by replacing: sizeof(email)/sizeof(char), with: strlen(email).
 
1 members found this post helpful.
Old 08-06-2023, 01:59 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,872
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Whenever you see `<name>(<number>)` enter command `man <number> <name>` e.g.`man 3 strlen`

Make sure not to call `strlen` within the loop:
Code:
int nlen= strlen(email);
for (int i = 0; i < nlen; i++) {
...
}

Last edited by NevemTeve; 08-06-2023 at 02:01 PM.
 
1 members found this post helpful.
Old 08-07-2023, 02:16 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,363

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
To expand on the above, the (3) means section 3 of the man pages https://www.cyberciti.biz/faq/howto-...nix-man-pages/
 
2 members found this post helpful.
Old 08-07-2023, 09:20 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,676
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
By far the easiest and clearest way to do this is with a regular expression engine. And you can readily find regular expression texts which work in a variety of contributed libraries.
 
Old 08-07-2023, 12:41 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by sundialsvcs View Post
By far the easiest and clearest way to do this is with a regular expression engine. And you can readily find regular expression texts which work in a variety of contributed libraries.
This is a bad recommendation for someone learning about the fundamentals of finite state machines & parsing.
 
  


Reply

Tags
c-language



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find the number of lookahead characters, needed by Lex for given DFA, for scanning input. ajiten Programming 25 10-17-2023 02:58 AM
LXer: How to Check the Available Network Interfaces, Associated IP Addresses, MAC Addresses, and Interface Speed on Linux LXer Syndicated Linux News 0 06-08-2020 08:13 AM
Need guidance on: new email provider that permits Linux-based IMAP connections; personal domain for persistent email addresses funkapus Linux - General 7 12-19-2018 09:42 AM
XML to DFA converter lalitcoep Linux - Software 4 10-20-2010 04:02 AM
need to know how to copy a DFA onto a matrics matthew d Programming 1 05-03-2006 08:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration