Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello this is my assignment i have to write a c program to split an email into main and domain name.
please anyone can help me to write a code that split the email.  


What I have tried:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
   main() 
 {
	FILE*fp=fopen("m.csv","r+");
     const char s[2]=",";              // here s is constant. It can't be reasign another string but we can assign another character value at s[index]
	char*token;                        //  Char* is a pointer that points to a token. Tokens are the smallest units that make a complete C program
	int i;
	if(fp!=NULL) {
		char line[30];
		while(fgets(line,sizeof line,fp)!=NULL) {
			token=strtok(line,s);    // Strtok break the String into tokens. It search for comma and when it is found it stores the preceding value into an array and so on 
			for(i=0;i<2;i++) {                        
				if(i==0) {
					printf("%s",token);
					token=strtok(NULL,s);
				} else {
					printf("%s",(token));
				}
			}
		}
		fclose(fp);
	} 
}
Posted
Updated 22-May-17 8:11am
v2
Comments
Richard MacCutchan 22-May-17 12:51pm    
What is the format of the 'email' that you need to split?
[no name] 22-May-17 13:02pm    
What is wrong with the code you have?
ZurdoDev 22-May-17 13:59pm    
Where are you stuck?

1 solution

That code has nothing to do with emails: it is to do with (very primative) CSV handling - even the comments agree.
If you mean that you need to split an email address "xxx@yyy.zzz" into two parts: "xxx" and "yyy.zzz" then that's relatively simple, provided you look at the rules for email addresses. This gives them nice and clearly: Email address - Wikipedia[^] In practice, you can probably ignore that the local part of an address may contain '@' - I've never seen one, nor have I seen a system set up to accept one. If you do decide to permit them, it's still not complex: start from the right hand end of the string, and search back for the last '@'. Since a Domain name cannot contain the '@' characters, that will always mark the separation point.

So: it's not complex: Copy the string into a new array, and locate the '@' character which separates the local and domain parts. Replace it with a null character '\0'.
The pointer address after the null is the start of the domain, the start of the array you copied into is the local address.
Done.

But this is your homework, so writing the code is up to you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900